asp.net - SignalR 2.0 超时连接

标签 asp.net timeout signalr

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

几分钟后超时消失并且不起作用。

我怎么办 在 SignalR 2.0 中设置超时连接 ?

最佳答案

您可以在 Owin Startup 类中使用以下配置。

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

此外,当您想让客户端始终连接到服务器时,您可以尝试在断开集线器事件中连接它。
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");
            });

关于asp.net - SignalR 2.0 超时连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21496509/

相关文章:

django - 处理长请求

concurrency - 如何在 Erlang 中对方法应用超时?

asp.net-mvc-4 -/signalr/hubs 未在 asp.net mvc4 : Throws 404 中加载

ASP.NET - 有条件地链接 CSS 文件

javascript - 选中标题复选框时获取每个选中行的特定单元格值

asp.net - 使用 MVC5 ASP.NET 和rackspace 发送电子邮件

c# - 如何根据框架执行不同的 Action ? (ASP.NET 5)

Spring WebSocket超时设置

c# - 使用 SignalR 发送 JSON

c# - 用于网络聊天的 WCF、SignalR 或 websockets