即使 SignalR SelfHost 服务器上的连接未关闭,是否可能会触发 OnDisconnected 事件?

Is it possible that OnDisconnected event will be fired even though connection is not closed on SignalR SelfHost server?

我们创建了一个自托管 SignalR 服务器,我们的 WPF 桌面应用程序正在连接到该服务器。我们只是在做登录-注销管理,一切都很简单而且有效。但是过了一段时间,通常是 2-3 小时,一半的客户从在线客户列表中消失了。

看起来,OnDisconnected 事件在服务器上触发以进行连接,即使它们没有关闭。在 OnDisconnected 事件之后,这些连接仍然可以发送和接收数据。

是否有任何情况会导致 OnDisconnected 在连接未关闭时在服务器上触发事件?

Is there any scenario that causes to OnDisconnected event triggered on server when connection is not closed?

是的。如果连接超时,首先断开连接然后重新连接(如果客户端没有明确关闭连接)。

public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled)
{
    if (stopCalled)
    {
        Console.WriteLine(String.Format("Client {0} explicitly closed the connection.", Context.ConnectionId));
    }
    else
    {
        Console.WriteLine(String.Format("Client {0} timed out .", Context.ConnectionId));
    }

    return base.OnDisconnected();
}

如您所见。如果发生显式断开连接(stopCalled 为真),客户端肯定会断开连接并且不会发生重新连接。

如果客户端确实没有断开连接,断开连接后可能会像您的情况一样发生重新连接。

检查 here 了解更多详情