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

标签 c# signalr self-hosting

我们创建了一个自托管 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更多细节

关于c# - 即使 SignalR SelfHost 服务器上的连接未关闭,是否可能会触发 OnDisconnected 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36448353/

相关文章:

wcf - WCF RIA 服务可以自托管吗?

WCF 仅本地 NamedPipe

authentication - 自托管 Web Api 中的 OWIN Cookie 身份验证

c# - Entity Framework - 将现有实体添加到种子方法中的一对一关系

c# - 在 C# 对象结构中连接字符串

asp.net-mvc - 使用 Asp.net 4.6 的 Azure SignalR 服务

asp.net-mvc - SignalR 客户端在每次刷新页面时断开/连接。 ASP.NET MVC

c# - 在 Web API 中解密不记名 token

C#远程web请求证书报错

c# - 运算符 == 不能应用于 C# 中的泛型类型吗?