Skip to content

Commit 2e7bc45

Browse files
committed
demo5 - rxjs 6
1 parent efe41c5 commit 2e7bc45

File tree

16 files changed

+8320
-0
lines changed

16 files changed

+8320
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Threading.Tasks;
2+
using demo5.Hubs;
3+
using demo5.Models;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.AspNetCore.SignalR;
6+
7+
namespace demo4.Controllers
8+
{
9+
10+
[ApiController]
11+
[Route("/message")]
12+
public class MessageController : Controller
13+
{
14+
public IHubContext<ApplicationHub> _hubContext { get; }
15+
16+
public MessageController(IHubContext<ApplicationHub> hubContext)
17+
{
18+
_hubContext = hubContext;
19+
}
20+
21+
[HttpPost]
22+
public Task PostMessage(ChatMessage message)
23+
{
24+
return _hubContext.Clients.All.SendAsync("Send", message.Message);
25+
}
26+
}
27+
}

src/demo5/Hubs/ApplicationHub.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.AspNetCore.SignalR;
2+
using System.Threading.Tasks;
3+
using demo5.Models;
4+
using System.Threading.Channels;
5+
using System;
6+
7+
namespace demo5.Hubs
8+
{
9+
public class ApplicationHub : Hub
10+
{
11+
public Task Send(ChatMessage message){
12+
return Clients.All.SendAsync("Send", message.Message);
13+
}
14+
15+
public ChannelReader<int> CountDown(int count) {
16+
var channel = Channel.CreateUnbounded<int>();
17+
18+
_ = WriteToChannel(channel.Writer, count);
19+
20+
return channel.Reader;
21+
22+
async Task WriteToChannel(ChannelWriter<int> writer, int thing) {
23+
for (int i = thing; i >= 0 ; i--)
24+
{
25+
await writer.WriteAsync(i);
26+
await Task.Delay(TimeSpan.FromSeconds(0.75));
27+
}
28+
29+
writer.Complete();
30+
}
31+
}
32+
}
33+
}

src/demo5/Models/ChatMessage.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace demo5.Models
2+
{
3+
public class ChatMessage
4+
{
5+
public string Message { get; set; }
6+
}
7+
}

src/demo5/Program.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore;
7+
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.Logging;
10+
11+
namespace demo5
12+
{
13+
public class Program
14+
{
15+
public static void Main(string[] args)
16+
{
17+
CreateWebHostBuilder(args).Build().Run();
18+
}
19+
20+
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
21+
WebHost.CreateDefaultBuilder(args)
22+
.UseStartup<Startup>();
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:9016",
7+
"sslPort": 44344
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"demo5": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
22+
"environmentVariables": {
23+
"ASPNETCORE_ENVIRONMENT": "Development"
24+
}
25+
}
26+
}
27+
}

src/demo5/Startup.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using demo5.Hubs;
6+
using Microsoft.AspNetCore.Builder;
7+
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.AspNetCore.Http;
9+
using Microsoft.Extensions.DependencyInjection;
10+
11+
namespace demo5
12+
{
13+
public class Startup
14+
{
15+
// This method gets called by the runtime. Use this method to add services to the container.
16+
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
17+
public void ConfigureServices(IServiceCollection services)
18+
{
19+
services.AddMvc();
20+
services.AddSignalR()
21+
.AddMessagePackProtocol() ;
22+
}
23+
24+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
25+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
26+
{
27+
if (env.IsDevelopment())
28+
{
29+
app.UseDeveloperExceptionPage();
30+
}
31+
32+
app.UseFileServer();
33+
app.UseMvc();
34+
app.UseSignalR(routes =>
35+
{
36+
routes.MapHub<ApplicationHub>("/app");
37+
});
38+
}
39+
}
40+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<template>
2+
<div>
3+
<h2>Hub API</h2>
4+
<form v-on:submit.prevent="addMessage">
5+
<input type="text" v-model="newMessage">
6+
<input type="submit" value="Send">
7+
</form>
8+
9+
<h2>REST API</h2>
10+
<form v-on:submit.prevent="addRestMessage">
11+
<input type="text" v-model="newRestMessage">
12+
<input type="submit" value="Send">
13+
</form>
14+
15+
<h2>Streaming Hub</h2>
16+
<form v-on:submit.prevent="countDown">
17+
<input type="text" v-model="number">
18+
<input type="submit" value="Send">
19+
</form>
20+
21+
<ul>
22+
<li v-for="(message, index) in messages" :key="index">{{message}}</li>
23+
</ul>
24+
</div>
25+
</template>
26+
27+
<script lang="ts">
28+
import { HubConnectionBuilder, HubConnection, LogLevel } from "@aspnet/signalr";
29+
import { MessagePackHubProtocol } from "@aspnet/signalr-protocol-msgpack";
30+
31+
import Vue from "vue";
32+
import { Component } from "vue-property-decorator";
33+
34+
import { map, filter, switchMap } from 'rxjs/operators';
35+
import {adapt} from './stream-adapter';
36+
37+
@Component({})
38+
export default class MainComponent extends Vue {
39+
messages: string[] = [];
40+
newMessage: string = "";
41+
newRestMessage: string = "";
42+
number: string = "";
43+
connection: HubConnection = null;
44+
45+
created() {
46+
this.connection = new HubConnectionBuilder()
47+
.configureLogging(LogLevel.Information)
48+
.withUrl("/app")
49+
.withHubProtocol(new MessagePackHubProtocol())
50+
.build();
51+
52+
console.log(this.connection);
53+
54+
this.connection.on("Send", message => {
55+
this.messages.push(message);
56+
});
57+
58+
this.connection.start().catch(error => console.error(error));
59+
}
60+
61+
async addMessage() {
62+
await this.connection.invoke("Send", { Message: this.newMessage });
63+
this.newMessage = null;
64+
}
65+
async addRestMessage() {
66+
await fetch("/message", {
67+
method: "post",
68+
body: JSON.stringify({ Message: this.newRestMessage }),
69+
headers: {
70+
"content-type": "application/json"
71+
}
72+
});
73+
this.newRestMessage = null;
74+
}
75+
76+
async countDown() {
77+
var stream = this.connection.stream<string>("CountDown", parseInt(this.number));
78+
var messages = this.messages;
79+
80+
adapt(stream).pipe(
81+
filter(x => parseInt(x) % 2 === 0)
82+
).subscribe(x => messages.push(x));
83+
84+
this.number = null;
85+
}
86+
}
87+
</script>

src/demo5/client/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Vue from "vue";
2+
import MainComponent from './main-component.vue';
3+
4+
new Vue({ el: "#app", components: { MainComponent } });

src/demo5/client/stream-adapter.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { IStreamResult } from "@aspnet/signalr";
2+
import { Subject, Observable } from "rxjs";
3+
4+
export function adapt<T = any>(stream: IStreamResult<T>): Observable<T> {
5+
const subject = new Subject<T>();
6+
stream.subscribe(subject);
7+
return subject.asObservable();
8+
}

src/demo5/demo5.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<TargetFramework>netcoreapp2.1</TargetFramework>
4+
</PropertyGroup>
5+
6+
<ItemGroup>
7+
<Folder Include="wwwroot\" />
8+
</ItemGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.AspNetCore.App" />
12+
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="1.0.0-rc1-final" />
13+
</ItemGroup>
14+
</Project>

0 commit comments

Comments
 (0)