Skip to content

Instantly share code, notes, and snippets.

@bitbonk
Created November 6, 2018 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitbonk/f89aa4324546f7dfd7108c1712248fd8 to your computer and use it in GitHub Desktop.
Save bitbonk/f89aa4324546f7dfd7108c1712248fd8 to your computer and use it in GitHub Desktop.
very simple signalr server + client
class Program
{
static async Task Main(string[] args)
{
var connection = new HubConnectionBuilder().WithUrl("http://localhost:19715/chat")
.Build();
await connection.StartAsync();
var receiveCounter = 0;
var isReceiver = args.Length < 1 || args[0].ToLower() != "sender";
if (isReceiver)
{
var stopWatch = new Stopwatch();
connection.On<int>("Send",
(id) =>
{
if (!stopWatch.IsRunning)
{
stopWatch.Start();
}
receiveCounter++;
if (receiveCounter % 1000 == 0)
{
Console.WriteLine($"Receiving {receiveCounter} ({stopWatch.Elapsed})");
}
});
if (receiveCounter >= 10000000)
{
Console.WriteLine($"It took {stopWatch.Elapsed} to receive {receiveCounter} values");
}
Console.ReadLine();
return;
}
for (var i = 0; i < 10000000; i++)
{
if (i % 1000 == 0)
{
Console.WriteLine($"Sending {i}");
}
await connection.InvokeAsync("Send", i);
}
Console.ReadLine();
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSignalR().AddMessagePackProtocol();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSignalR(routes => { routes.MapHub<Chat>("/chat"); });
app.Run(async context => { await context.Response.WriteAsync("Hello world!"); });
}
}
public class Chat : Hub
{
public Task Send(int id)
{
return this.Clients.All.SendAsync("Send", id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment