Skip to content

Instantly share code, notes, and snippets.

@glennc
Last active November 10, 2017 07:51
Show Gist options
  • Save glennc/aae6b4496c43316d2f26e82516397dc8 to your computer and use it in GitHub Desktop.
Save glennc/aae6b4496c43316d2f26e82516397dc8 to your computer and use it in GitHub Desktop.
SignalRAnnouncement
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");
let connection = new signalR.HubConnection('/chat');
connection.on('send', data => {
console.log(data);
});
connection.start()
.then(() => connection.invoke('send', 'Hello'));
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);
}
}
}
npm install @aspnet/signalr-client
public IObservable<Stock> StreamStocks()
{
return _stockTicker.StreamStocks();
}
<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>
<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>
<script src="scripts/signalr-client.min.js"></script>
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");
});
}
}
}
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}");
}
}
}
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