Signalr 2.0超时连接



我正在使用signalr。但是超时存在问题。

超时几分钟后消失,无法正常工作。

如何在Signalr 2.0

中设置超时连接

您可以在OWIN启动类中使用以下配置。

        // Make long polling connections wait a maximum of 110 seconds for a
        // response. When that time expires, trigger a timeout command and
        // make the client reconnect.
        GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(40);
        // Wait a maximum of 30 seconds after a transport connection is lost
        // before raising the Disconnected event to terminate the SignalR connection.
        GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(30);
        // For transports other than long polling, send a keepalive packet every
        // 10 seconds. 
        // This value must be no more than 1/3 of the DisconnectTimeout value.
        GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(10);
        //Setting up the message buffer size
        GlobalHost.Configuration.DefaultMessageBufferSize = 500;

还要将客户端一直连接到服务器时,也可以尝试在Disconnect Hub事件中连接它。

var tryingToReconnect = false;
$.connection.hub.disconnected(function () {
                //TODO: write the logic to reconnect to server.
                if(!tryingToReconnect) {
                    // notifyclient about disconnection
                    setTimeout(function() {
                        $.connection.hub.start();
                    }, 5000); // Restart connection after 5 seconds.
                }
            });
           $.connection.hub.reconnecting(function() {
                tryingToReconnect = true;
                console.log("reconnecting...");
            });
            $.connection.hub.reconnected(function() {
                tryingToReconnect = false;
                console.log("Reconnected");
            });

相关内容

  • 没有找到相关文章

最新更新