Created
March 13, 2024 22:27
-
-
Save cheoalfredo/e31a0e112c46fee6c93502d325a225e2 to your computer and use it in GitHub Desktop.
Webapi example of SignalR usage
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
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