As I know, The OnReconnected event handler in a SignalR Hub can execute directly after OnConnected but not after OnDisconnected for a given client. (source: http://www.asp.net/signalr/overview/guide-to-the-api/handling-connection-lifetime-events)
So, if onReconnected will never be happened after onDisconnected and context.connectionId will remain the same, why official example checks context.connectionId in user connections and add it if not exists.
Link: http://www.asp.net/signalr/overview/guide-to-the-api/mapping-users-to-connections
答案 0 :(得分:2)
public override Task OnDisconnected(bool stopCalled)
{
string name = Context.User.Identity.Name;
_connections.Remove(name, Context.ConnectionId);
return base.OnDisconnected(stopCalled);
}
如果正常断开连接,则stopCalled将为true。否则,它将是假的(超时等),但这并不意味着信号器肯定会断开连接。
如果SignalR位于配置了scaleout的负载均衡器后面,那么 客户端可能仍然连接到另一个SignalR服务器。
即使客户端未断开连接,也可以触发OnDisconnected(false)
。然后删除Context.ConnectionId
。
但是如果客户端仍然连接,那么将重置连接。因此,您应该检查的时间可能是您使用OnDisconnected(false)
删除了此连接,这不是实际断开连接。