Files
ASPReactDemo/Api/SignalR/SignalRSendService.cs
Frederic Beckmann c4071786ea Implement SignalR integration and refactor WeatherPage
Added SignalR for real-time progress updates during weather data fetch. Refactored WeatherPage to use a new reusable WeatherGrid component and SignalRHelper. Improved loading UI with a radial progress indicator.
2025-02-09 02:00:06 +01:00

31 lines
1.1 KiB
C#

using Microsoft.AspNetCore.SignalR;
namespace Api.SignalR
{
public class SignalRSendService(IHubContext<WeatherUpdateHub> hubContext, ILogger<SignalRSendService> logger) : BackgroundService
{
#region Override ExecuteAsync
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
// For example, wait 10 seconds between messages.
await Task.Delay(TimeSpan.FromSeconds(2), stoppingToken);
// Log or do any work here.
logger.LogInformation("Background service sending message at: {Time}", DateTime.Now);
// Send a message to all connected clients.
await hubContext.Clients.All.SendAsync(
"ReceiveMessage", // This is the client method name.
"Background Service", // Example sender.
$"Hello from the background task at {DateTime.Now:F}", // The message.
stoppingToken);
}
}
#endregion
}
}