【问题标题】:Detect SignalR Hub Client Disconnect instantly立即检测 SignalR Hub 客户端断开连接
【发布时间】:2013-08-06 08:52:57
【问题描述】:

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

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

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

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

【问题讨论】:

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

标签: .net signalr-hub signalr.client


【解决方案1】:

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

当连接处于非活动状态时,服务器会定期发送一个 给客户端的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();
}

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

【讨论】:

  • 嗨,杰森,感谢您提供的信息。但从我的测试来看,KeepAlive 只影响 Hub 客户端代理时间以引发 StateChanged 事件。我已经尝试在服务器端将 ConnectionTimeout 设置为 6 秒,将 KeepAlive 设置为 2 秒,但它对 Hub OnDisconnected 方法没有影响。
  • 没问题,很高兴能帮上忙 :)
【解决方案2】:

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

由于 TCP 的工作方式,在您尝试向该客户端发送数据之前,您不能这样做。正如@JasonEvans 的回答所解释的,SignalR 默认每十秒发送一次数据(“keepalive”消息)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    相关资源
    最近更新 更多