2013-08-06 52 views
5

当是SignalR中心OnDisconnected提出在服务器端,为.NET客户端是崩溃或关闭,而不调用停止方法?检测SignalR集线器客户端断开瞬间

我正在测试SignalR .NET客户端,而不是javascript客户端。 如果我叫停止方法在客户端上,集线器将提高立即OnDisconnected方法。

但是如果我关闭客户端或杀死的过程中,中心将提高仅约10秒后OnDisconnected方法。

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

+0

嗨@JasonEvans,感谢您的链接。 –

回答

5

读了文档SignalR events here,我发现这个部分:

当连接是无效的,周期性地向服务器发送一个 保活分组发送到客户端。作为这种制品被 写入日期,默认频率为每10秒

有描述如何改变存活例如设定部

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

因此,您可以考虑减少该值,以便在客户端连接断开时更快得到通知。

+0

嗨杰森,谢谢你的信息。但是从我的测试中,KeepAlive仅影响Hub客户端代理时间来提升StateChanged事件。我已经尝试在服务器端将ConnectionTimeout设置为6秒并将KeepAlive设置为2秒,但它对Hub OnDisconnected方法没有影响。 –

+0

没问题,很高兴我可以帮忙:) –

3

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

由于TCP的工作方式,您不能尝试将数据发送到该客户端。正如@JasonEvans的答案所解释的那样,SignalR默认每10秒发送一次数据(“keepalive”消息)。

相关问题