如何增加ASP.NET Core SignalR v2.1中的超时设置? [英] How to increase timeout setting in ASP.NET Core SignalR v2.1?
问题描述
我正在尝试在ASP.NET Core 2.1上使用最新的SignalR.我有基本的应用程序正在运行,但现在很快就会超时.我看到此错误-
I'm trying out the latest SignalR on ASP.NET Core 2.1. I have the basic app working but it times out pretty soon right now. I see this error -
错误:连接已断开,错误为错误:服务器超时,但未收到来自服务器的消息."
Error: Connection disconnected with error 'Error: Server timeout elapsed without receiving a message from the server.'.
在哪里可以更改此超时设置?我在此处尝试了文档,但没有任何信息关于那里的超时. 我正在Windows 10的开发环境中运行并使用JavaScript客户端.
Where can this timeout setting be changed? I tried the docs here but no info about timeout there. I'm running on a dev environment of Windows 10 and using JavaScript client.
启用客户端日志记录后插入图像.
Inserting image after enabling client logging.
Edit 2: Link to page containing timeouts about the older SignalR version.
推荐答案
针对将来可能会来到这里的人们:
For people that may come here in future:
以下是增加Javascript客户端超时的方法
Here is how to increase Javascript client timeout
hubConnection.serverTimeoutInMilliseconds = 100000; // 100 second
But also take a look at this link and then read my comments below:
如果signalR断开连接,则应尝试重新建立连接.连接可能由于其他一些原因而断开,包括用户交换网络.例如,如果用户正在使用手机并连接到家庭/办公室wifi,但先退出,然后再连接到蜂窝数据连接.
If signalR got disconnected, you should try re-establishing the connection again. The connection could drop for several other reasons, including the user switching networks. For example if the user is using a cell-phone and connected to the home/office wifi but steps out which then connects to cellular data connection.
要重新连接,您可以使用以下命令(对我来说就像是护身符):
To reconnect you could use the following (works like a charm for me):
// re-establish the connection if connection dropped
connection.onclose(() => setTimeout(startSignalRConnection(connection), 5000));
其中startSignalRConnection
是:
const startSignalRConnection = connection => connection.start()
.then(() => console.info('Websocket Connection Established'))
.catch(err => console.error('SignalR Connection Error: ', err));
连接是
const connection = new HubConnectionBuilder()
.withUrl(connectionHub, options)
.withHubProtocol(protocol)
.build();
这篇关于如何增加ASP.NET Core SignalR v2.1中的超时设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!