SignalR客户端由于不活动而停止连接

时间:2017-04-20 07:59:18

标签: connection timeout signalr

我在控制台应用程序和WPF客户端中有一个signalR集线器。 在启动时,signalR连接初始化,并应始终保持活动状态。 每秒使用当前的SystemTime将消息从服​​务器推送到客户端(这是您可以在日志中看到的' SystemTimeUpdateNotification')

我现在描述的行为是在Windows7设备上:每隔一段时间我就会看到客户端失去与服务器的连接。 由于某种原因,客户端检测到它已不活动并停止连接。我不知道自2017年4月20日凌晨3:04:06以来在哪里不活动'并且'超时00:00:50'是来自。 如果我在同一时间检查服务器上的日志,我可以看到没有keepalive消息。

    2017-04-20 05:04:04.0728,Trace,Received notification SystemTimeUpdateNotification
    2017-04-20 05:04:05.0859,Trace,Received notification SystemTimeUpdateNotification
    2017-04-20 05:04:06.0999,Trace,Received notification SystemTimeUpdateNotification
    2017-04-20 05:05:13.1020,Trace,Received notification SystemTimeUpdateNotification
    2017-04-20 05:05:14.1160,Trace,Received notification SystemTimeUpdateNotification
    2017-04-20 05:05:14.5511,Info,The client has been inactive since 4/20/2017 3:04:06 AM and it has exceeded the inactivity timeout of 00:00:50. Stopping the connection.
    2017-04-20 05:05:14.5871,Info,SignalR connectionstate changed from 'Connected' to 'Disconnected'
    2017-04-20 05:05:14.5871,Trace,Received notification NotificationConnectionStateChanged
    2017-04-20 05:05:14.5981,Info,Disconnecting...
    2017-04-20 05:05:14.5981,Info,Cancel ongoing operations
    2017-04-20 05:05:19.6104,Info,SignalR connectionstate changed from 'Disconnected' to 'Connecting'
    2017-04-20 05:05:19.6104,Trace,Received notification NotificationConnectionStateChanged
    2017-04-20 05:05:19.6104,Info,Disconnecting...
    2017-04-20 05:05:19.6454,Info,SignalR connectionstate changed from 'Connecting' to 'Connected'
    2017-04-20 05:05:19.6454,Trace,Received notification NotificationConnectionStateChanged
    2017-04-20 05:05:19.6454,Info,Connecting...
    2017-04-20 05:05:19.6524,Debug,Performing client/server compatibility check
    2017-04-20 05:05:19.6524,Debug,Software compatible
    2017-04-20 05:05:19.6524,Info,Start retrieving initial data
    2017-04-20 05:05:20.0344,Info,initial data retrieved
    2017-04-20 05:05:20.2044,Trace,Received notification SystemTimeUpdateNotification
    2017-04-20 05:05:21.2144,Trace,Received notification SystemTimeUpdateNotification

登录服务器端:

    2017-04-20 05:03:31.6069;Trace;KeepAlive(6a12952a-1cb4-4933-b6b1-db16158958a9)
    2017-04-20 05:03:41.6225;Trace;KeepAlive(6a12952a-1cb4-4933-b6b1-db16158958a9)
    2017-04-20 05:03:51.6371;Trace;KeepAlive(6a12952a-1cb4-4933-b6b1-db16158958a9)
    2017-04-20 05:04:01.6527;Trace;KeepAlive(6a12952a-1cb4-4933-b6b1-db16158958a9)
    2017-04-20 05:05:14.5661;Info;Abort(6a12952a-1cb4-4933-b6b1-db16158958a9)
    2017-04-20 05:05:14.5661;Info;Removing connection 6a12952a-1cb4-4933-b6b1-db16158958a9
    2017-04-20 05:05:14.5661;Info;End(6a12952a-1cb4-4933-b6b1-db16158958a9)
    2017-04-20 05:05:14.5841;Trace;DrainWrites(6a12952a-1cb4-4933-b6b1-db16158958a9)
    2017-04-20 05:05:14.5841;Info;CompleteRequest (6a12952a-1cb4-4933-b6b1-db16158958a9)
    2017-04-20 05:05:19.6344;Info;Connection 9249a8a5-8653-450c-b8da-c5b7a4c7df81 is New.
    2017-04-20 05:05:27.6738;Trace;KeepAlive(9249a8a5-8653-450c-b8da-c5b7a4c7df81)
    2017-04-20 05:05:37.6874;Trace;KeepAlive(9249a8a5-8653-450c-b8da-c5b7a4c7df81)

现在我找到了非常奇怪的部分。 '偶尔'其实很固定。我查看了过去几天的日志条目,似乎总是每11个小时左右

Inactivity  Timestamp   Difference with previous    LogEntry
17/04/2017  17:59:44    0:00:00                     The client has been inactive since 4/17/2017 3:58:35 PM and it has exceeded the inactivity timeout of 00:00:50. Stopping the connection.
17/04/2017  17:59:44    11:01:06                    The client has been inactive since 4/17/2017 3:58:35 PM and it has exceeded the inactivity timeout of 00:00:50. Stopping the connection.
17/04/2017  06:58:38    10:01:01                    The client has been inactive since 4/17/2017 4:57:34 AM and it has exceeded the inactivity timeout of 00:00:50. Stopping the connection.
16/04/2017  20:57:37    11:01:06                    The client has been inactive since 4/16/2017 6:56:29 PM and it has exceeded the inactivity timeout of 00:00:50. Stopping the connection.
16/04/2017  09:56:31    11:01:06                    The client has been inactive since 4/16/2017 7:55:23 AM and it has exceeded the inactivity timeout of 00:00:50. Stopping the connection.
15/04/2017  22:55:25    11:01:06                    The client has been inactive since 4/15/2017 8:54:17 PM and it has exceeded the inactivity timeout of 00:00:50. Stopping the connection.
15/04/2017  11:54:19    0:00:00                     The client has been inactive since 4/15/2017 9:53:09 AM and it has exceeded the inactivity timeout of 00:00:50. Stopping the connection.
15/04/2017  11:54:19    11:01:07                    The client has been inactive since 4/15/2017 9:53:09 AM and it has exceeded the inactivity timeout of 00:00:50. Stopping the connection.
15/04/2017  00:53:12    11:01:06                    The client has been inactive since 4/14/2017 10:52:03 PM and it has exceeded the inactivity timeout of 00:00:50. Stopping the connection.
14/04/2017  13:52:06    11:01:06                    The client has been inactive since 4/14/2017 11:50:58 AM and it has exceeded the inactivity timeout of 00:00:50. Stopping the connection.
14/04/2017  02:51:00                                The client has been inactive since 4/14/2017 12:49:52 AM and it has exceeded the inactivity timeout of 00:00:50. Stopping the connection.

是否存在最大连接时间或可能导致此行为的原因?

编辑1 : 如果我在Windows 8上运行相同的代码,我就不会遇到这个问题。 我想这与Windows 7上使用的传输方法有关(不支持websockets)

编辑2

Server  Client  Inactivity problem
Win 8   Win 8   No
Win 7   Win 7   Yes
Win 7   Win 8   No
Win 8   Win 7   Yes

所以它似乎与客户端的操作系统有关。

1 个答案:

答案 0 :(得分:0)

正如documentation中所述,保持连接的一种方法是在断开连接后不断重新连接。在客户端中设置已断开连接的功能

$.connection.hub.disconnected(function() {
   setTimeout(function() {
       $.connection.hub.start();
   }, 5000); // Restart connection after 5 seconds.
});