Last active
November 10, 2017 07:51
-
-
Save glennc/aae6b4496c43316d2f26e82516397dc8 to your computer and use it in GitHub Desktop.
SignalRAnnouncement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var connection = new HubConnectionBuilder() | |
| .WithUrl("http://localhost:5000/chat") | |
| .WithConsoleLogger() | |
| .Build(); | |
| connection.On<string>("Send", data => | |
| { | |
| Console.WriteLine($"Received: {data}"); | |
| }); | |
| await connection.StartAsync(); | |
| await connection.InvokeAsync("Send", "Hello"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let connection = new signalR.HubConnection('/chat'); | |
| connection.on('send', data => { | |
| console.log(data); | |
| }); | |
| connection.start() | |
| .then(() => connection.invoke('send', 'Hello')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Threading.Tasks; | |
| using Microsoft.AspNetCore.SignalR; | |
| namespace Sample | |
| { | |
| public class Chat : Hub | |
| { | |
| public Task Send(string message) | |
| { | |
| return Clients.All.InvokeAsync("Send", message); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| npm install @aspnet/signalr-client |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public IObservable<Stock> StreamStocks() | |
| { | |
| return _stockTicker.StreamStocks(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Project Sdk="Microsoft.NET.Sdk.Web"> | |
| <PropertyGroup> | |
| <TargetFramework>netcoreapp2.0</TargetFramework> | |
| </PropertyGroup> | |
| <ItemGroup> | |
| <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" /> | |
| <PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.0.0-alpha1-final" /> | |
| </ItemGroup> | |
| <ItemGroup> | |
| <Folder Include="wwwroot\scripts\" /> | |
| </ItemGroup> | |
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Project Sdk="Microsoft.NET.Sdk"> | |
| <PropertyGroup> | |
| <OutputType>Exe</OutputType> | |
| <TargetFramework>netcoreapp2.0</TargetFramework> | |
| </PropertyGroup> | |
| <ItemGroup> | |
| <PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="1.0.0-alpha1-final" /> | |
| </ItemGroup> | |
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script src="scripts/signalr-client.min.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Microsoft.AspNetCore.Builder; | |
| using Microsoft.Extensions.DependencyInjection; | |
| namespace Sample | |
| { | |
| public class Startup | |
| { | |
| public void ConfigureServices(IServiceCollection services) | |
| { | |
| services.AddSignalR(); | |
| } | |
| public void Configure(IApplicationBuilder app) | |
| { | |
| app.UseSignalR(routes => | |
| { | |
| routes.MapHub<Chat>("chat"); | |
| }); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private async Task StartStreaming() | |
| { | |
| var channel = connection.Stream<Stock>("StreamStocks", CancellationToken.None); | |
| while (await channel.WaitToReadAsync()) | |
| { | |
| while (channel.TryRead(out var stock)) | |
| { | |
| Console.WriteLine($"{stock.Symbol} {stock.Price}"); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function startStreaming() { | |
| connection.stream("StreamStocks").subscribe({ | |
| next: displayStocks, | |
| error: function (err) { | |
| logger.log(err); | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment