问题描述
我在使用 SignalR、ASP.Net Core 3.1、Kestrel 和 Angular 10 创建的应用程序时遇到了一些问题。希望有人能够帮助我。我在论坛上搜索了很长时间,但无法解决当前的错误。
该应用程序是仪表板类型 - 可以单击很少的组件来过滤实时数据。一些基本的 UI 组件将始终显示,并且应始终从集线器获取数据流。
启动设置:
services.AddSignalR(hubOptions =>
{
hubOptions.EnableDetailedErrors = true;
// Maybe I don’t need those,but connection is dropping very frequently with defaults.
hubOptions.KeepAliveInterval = TimeSpan.FromMinutes(1);
hubOptions.ClientTimeoutInterval = TimeSpan.FromSeconds(5);
hubOptions.ClientTimeoutInterval = TimeSpan.FromSeconds(15);
})
.AddNewtonsoftJsonProtocol();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<MyHub>("/data");
});
服务器设置:
webBuilder.ConfigureKestrel(serverOptions =>
{
serverOptions.Listen(IPAddress.Any,80,listenoptions =>
{
listenoptions.UseConnectionLogging();
});
serverOptions.Limits.KeepAliveTimeout = TimeSpan.FromDays(30);
serverOptions.Limits.MinRequestBodyDatarate = null;
serverOptions.Limits.MinResponseDatarate = null;
});
连接设置:
private buildHubConnection(uri: string): signalR.HubConnection {
return new signalR.HubConnectionBuilder()
.withUrl(uri)
.configureLogging(signalR.LogLevel.Debug)
.withAutomaticReconnect({nextRetryDelayInMilliseconds: () => 2000})
.build();
}
一起:
serverTimeoutInMilliseconds = 100000;
问题如下:
响应速度很慢,直到崩溃。服务器通过流将数据推送到仪表板上的每个组件,但它甚至会发生请求超时(我使用长轮询测试了该部分,但 Web 套接字存在相同的延迟)。
我希望来自服务器的数据会在正常时间被推回,这可以通过 UI 转换来覆盖。延迟太大,涉及到请求超时。
对于 Web 套接字,这是错误消息:
Error: Connection disconnected with error 'Error: Server timeout elapsed without receiving a message from the server.'
。
即使重新连接成功,也没有数据。
在大多数情况下,场景是重新连接尝试的长循环,并出现以下错误:
information: Connection reconnecting because of error 'Error: Server timeout elapsed without receiving a message from the server.'
。
即使在控制台日志成功重新连接后,dataQuery 也失败说:
Error: Cannot send data if the connection is not in the 'Connected' State.
这一切对我来说是不合逻辑的。如果服务器应该流式传输数据,为什么会关闭连接?你能帮我弄清楚可能缺少什么,或者可能是什么问题?感谢任何人的回复。
解决方法
连接因错误“错误:服务器超时已过”而断开 没有收到来自服务器的消息。'
serverTimeoutInMilliseconds
的默认超时值为 30,000 毫秒(30 秒),如果超时后没有收到任何来自服务器的消息,连接可能会因服务器超时错误而终止。
services.AddSignalR(hubOptions =>
{
hubOptions.EnableDetailedErrors = true;
// Maybe I don’t need those,but connection is dropping very frequently with defaults.
hubOptions.KeepAliveInterval = TimeSpan.FromMinutes(1);
hubOptions.ClientTimeoutInterval = TimeSpan.FromSeconds(5);
hubOptions.ClientTimeoutInterval = TimeSpan.FromSeconds(15);
})
.AddNewtonsoftJsonProtocol();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<LiveDataHub>("/liveData");
});
问题可能与上述代码有关,您只需更新 SignalR 集线器的 KeepAliveInterval
设置,而不更改客户端的 serverTimeoutInMilliseconds
值。并且推荐的 serverTimeoutInMilliseconds
值是 KeepAliveInterval
值的两倍。更多详细信息,请参阅SignalR
Configure server options