Created
November 6, 2018 16:27
-
-
Save bitbonk/f89aa4324546f7dfd7108c1712248fd8 to your computer and use it in GitHub Desktop.
very simple signalr server + client
This file contains 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
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(); | |
} | |
} |
This file contains 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 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