SignalR 2.0连接超时 [英] SignalR 2.0 Timeout Connection

查看:4427
本文介绍了SignalR 2.0连接超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SignalR.But有一个关于超时问题。

I am using SignalR.But there is a problem about timeout.

超时几分钟后消失,不工作。

Timeout disappears after a few minutes and does not work.

我怎么能在SignalR 2.0设置超时连接

推荐答案

您可以在您的Owin启动类使用以下配置。

You can use the following configuration in your Owin Startup class.

        // 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;

此外,当你要保持你的客户端连接到服务器的时候,你可以尝试将其连接在断开连接枢纽的事件。

Also when you want to keep your client connected to server all the time, you can try to connect it in disconnect hub event.

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");
            });

这篇关于SignalR 2.0连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆