检测SignalR Hub客户端立即断开连接

Joh*_*Hpa 5 .net signalr-hub signalr.client

何时在服务器端引发SignalR Hub OnDisconnected,对于崩溃或关闭而没有调用Stop方法的.net客户端?

我正在测试SignalR .NET客户端,而不是javascript客户端.如果我在客户端上调用Stop方法,Hub将立即引发OnDisconnected方法.

但是如果我关闭客户端或终止进程,Hub将在大约10秒后才会引发OnDisconnected方法.

如何立即检测到客户端已断开连接?

Jas*_*ans 5

在这里阅读了SignalR事件的文档后,我发现了这一部分:

当连接处于非活动状态时,服务器会定期向客户端发送keepalive数据包.截至本文撰写之日,默认频率为每10秒

有一节介绍如何更改keepalive设置,例如

protected void Application_Start(object sender, EventArgs e)
{
    // 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(110);

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

    RouteTable.Routes.MapHubs();
}
Run Code Online (Sandbox Code Playgroud)

因此,您可以考虑减少该值,以便更快地通知客户端连接何时断开.