Skip to content

Instantly share code, notes, and snippets.

@cheoalfredo
Created March 13, 2024 22:27
Show Gist options
  • Save cheoalfredo/e31a0e112c46fee6c93502d325a225e2 to your computer and use it in GitHub Desktop.
Save cheoalfredo/e31a0e112c46fee6c93502d325a225e2 to your computer and use it in GitHub Desktop.
Webapi example of SignalR usage
using Microsoft.AspNetCore.SignalR;
using WebsocketTest;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSignalR();
var app = builder.Build();
app.UseHttpsRedirection();
var summaries = new[] {
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", async (IHubContext<WSTestHub> hub) =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
( DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
)).ToArray();
await hub.Clients.All.SendAsync("EndpointHit", "Endoint Weather forecast hit");
return forecast;
});
app.MapHub<WSTestHub>("/ws");
await app.RunAsync();
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment