asp.net – SignalR 2.0超时连接

我正在使用SignalR.But有一个关于超时的问题.

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

如何在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;

此外,当您希望始终将客户端连接到服务器时,可以尝试在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");
            });

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....