2013-08-06 54 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」消息)。

相關問題