为什么我们在发生signalR中的recconect时检查context.connectionId?
问题描述:
据我所知,SignalR Hub中的OnReconnected事件处理程序可以在OnConnected后直接执行,但不能在给定客户端的OnDisconnected后执行。 (来源:http://www.asp.net/signalr/overview/guide-to-the-api/handling-connection-lifetime-events)为什么我们在发生signalR中的recconect时检查context.connectionId?
因此,如果在onDisconnected之后永远不会发生onReconnected,并且context.connectionId将保持不变,为什么官方示例将检查用户连接中的context.connectionId并添加它(如果不存在)。
链接:http://www.asp.net/signalr/overview/guide-to-the-api/mapping-users-to-connections
答
public override Task OnDisconnected(bool stopCalled)
{
string name = Context.User.Identity.Name;
_connections.Remove(name, Context.ConnectionId);
return base.OnDisconnected(stopCalled);
}
如果出现正常断开,stopCalled将是真实的。否则,它将是错误的(超时等),但这并不意味着信号被断开。
如果SignalR位于配置了扩展输出的负载均衡器之后,则 客户端可能仍然连接到另一个SignalR服务器。
即使客户端没有断开连接,OnDisconnected(false)
可以被触发。然后您删除Context.ConnectionId
。
但是,如果客户端仍然连接,则重新连接将被触发。所以那个时候你应该检查一下可能是你用OnDisconnected(false)
删除了这个连接,这不是真正的断开。
非常感谢 –