Configure SignalR To Show Detailed Errors And Fallback To Long Polling If WebSocket Dies (To Diagnose Clientside Connection Errors)

This commit is contained in:
Alan Moon 2025-07-31 09:49:07 -07:00
parent 0b35f4e55e
commit a7dda238ce

View File

@ -24,7 +24,11 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers(); builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddDbContext<DataContext>(); builder.Services.AddDbContext<DataContext>();
builder.Services.AddSignalR(); builder.Services.AddSignalR().AddHubOptions<ChatHub>(options =>
{
options.EnableDetailedErrors = true;
options.KeepAliveInterval = TimeSpan.FromMinutes(1);
});
builder.Services.AddStackExchangeRedisCache(options => builder.Services.AddStackExchangeRedisCache(options =>
{ {
@ -79,7 +83,11 @@ app.UseAuthorization();
app.MapControllers(); app.MapControllers();
app.MapHub<ChatHub>("/chat"); app.MapHub<ChatHub>("/chat", options =>
{
options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransportType.WebSockets |
Microsoft.AspNetCore.Http.Connections.HttpTransportType.LongPolling;
});
app.MapHub<TicTacToeHub>("/tttgame"); app.MapHub<TicTacToeHub>("/tttgame");
app.Run(); app.Run();