【问题标题】:Why does SignalR client ConnectionState not change when I shut down the site and then restart?为什么当我关闭站点然后重新启动时 SignalR 客户端 ConnectionState 不会更改?
【发布时间】:2023-04-08 17:15:01
【问题描述】:

我在 IIS 中将 signalR 客户端作为网站托管。 我测试了从服务器发送消息到这个客户端,它工作正常。

然后我关闭客户端站点,并在 10 分钟后重新启动它

再一次,我向这个客户端发送了一条消息,它仍然有效。

我的问题是为什么连接状态没有改变

顺便说一句,我有状态改变事件回调方法

connection.StateChanged += (x) => {
    OnStateChange(connectionId, x);
};

private void OnStateChange(string connectionId, StateChange stateChange)
{
    StringBuilder text = new StringBuilder();
    text.Append("ConnectiId:").Append(connectionId).Append(Environment.NewLine);
    text.Append("OldState:").Append(stateChange.OldState.ToString()).Append(Environment.NewLine);
    text.Append("NewState:").Append(stateChange.NewState.ToString()).Append(Environment.NewLine);
    if(stateChange.NewState == ConnectionState.Disconnected)
    {
        //reconnect
    }
    Util.Log("signalR_statechange", text.ToString(), false);
}

当我关闭或重新启动网站时没有日志。

【问题讨论】:

    标签: c# iis signalr


    【解决方案1】:

    转到此线程阅读...下面的继续...也不需要 onstatechange 事件...阅读 msdn 链接它将为you提供更多帮助

    如何在 Hub 类中处理连接生命周期事件

    处理连接生命周期事件的典型原因是跟踪用户是否连接,并跟踪用户名和连接 ID 之间的关联。要在客户端连接或断开连接时运行您自己的代码,请覆盖 Hub 类的 OnConnected、OnDisconnected 和 OnReconnected 虚拟方法,如下例所示。

    public class ContosoChatHub : Hub
    {
        public override Task OnConnected()
        {
            // Add your own code here.
            // For example: in a chat application, record the association between
            // the current connection ID and user name, and mark the user as online.
            // After the code in this method completes, the client is informed that
            // the connection is established; for example, in a JavaScript client,
            // the start().done callback is executed.
            return base.OnConnected();
        }
    
        public override Task OnDisconnected()
        {
            // Add your own code here.
            // For example: in a chat application, mark the user as offline, 
            // delete the association between the current connection id and user name.
            return base.OnDisconnected();
        }
    
        public override Task OnReconnected()
        {
            // Add your own code here.
            // For example: in a chat application, you might have marked the
            // user as offline after a period of inactivity; in that case 
            // mark the user as online again.
            return base.OnReconnected();
        }
    }
    

    当 OnConnected、OnDisconnected 和 OnReconnected 被调用时

    每次浏览器导航到新页面时,都必须建立新连接,这意味着 SignalR 将执行 OnDisconnected 方法,然后执行 OnConnected 方法。 SignalR 总是在建立新连接时创建一个新的连接 ID。

    当 SignalR 可以自动恢复的连接暂时中断时调用 OnReconnected 方法,例如在连接超时之前暂时断开和重新连接电缆时。 OnDisconnected 方法在客户端断开连接且 SignalR 无法自动重新连接时调用,例如当浏览器导航到新页面时。因此,给定客户端的可能事件序列是 OnConnected、OnReconnected、OnDisconnected;或 OnConnected、OnDisconnected。您不会看到给定连接的 OnConnected、OnDisconnected、OnReconnected 序列。

    在某些情况下不会调用 OnDisconnected 方法,例如当服务器关闭或应用程序域被回收时。当另一台服务器上线或 App Domain 完成其回收时,一些客户端可能能够重新连接并触发 OnReconnected 事件。

    有关详细信息,请参阅了解和处理 SignalR 中的连接生命周期事件。

    调用者状态未填充

    连接生命周期事件处理程序方法是从服务器调用的,这意味着您在客户端的状态对象中放置的任何状态都不会填充到服务器的 Caller 属性中。有关 state 对象和 Caller 属性的信息,请参阅本主题后面的如何在客户端和 Hub 类之间传递状态。

    如何从 Context 属性中获取有关客户端的信息

    要获取有关客户端的信息,请使用 Hub 类的 Context 属性。 Context 属性返回一个 HubCallerContext 对象,该对象提供对以下信息的访问:

    调用客户端的连接ID。

    string connectionID = Context.ConnectionId;
    

    连接 ID 是 SignalR 分配的 GUID(您无法在自己的代码中指定该值)。每个连接都有一个连接 ID,如果您的应用中有多个 Hub,则所有 Hub 使用相同的连接 ID。

    【讨论】:

    • 但是这篇文章仍然没有解释为什么我关闭IIS站点后客户端没有重新连接或断开连接
    • 您需要在客户端连接ID中添加客户端浏览器的cookie然后才
    • 您在系统连接事件中确定了系统中连接的用户.. 并将所有聊天存储为本地数据库
    【解决方案2】:

    原因是:

    关闭iis后你是否关闭了客户端浏览器?

    我可以告诉你,即使你关闭了 iis,客户端仍在连接。

    因为signalr客户端html文件不需要托管在iis中,所以即使托管在客户端也可以连接。

    【讨论】:

    • 这不是一个 html 页面。我的 signalR 客户端是在 Global.asax 中启动的。
    • 你关闭浏览器了吗?浏览器未关闭时它应该仍在连接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多