【问题标题】:How to reconnect signalR properly after disconnected断开连接后如何正确重新连接signalR
【发布时间】:2018-12-09 23:38:01
【问题描述】:

在我的带有 ASP.NET 服务器应用程序的 angular6 网站中, 我希望 singnalR 客户端应始终与集线器(服务器)保持连接。为此,使用以下代码快照在断开连接的事件上开始连接。

    this.connection.start().done((data: any) => {
        console.log('Now connected');
        this.connectionId = this.connection.id;
        this.connectionEstablished.emit(true);
        this.connectionExists = true;
    }).fail((error: any) => {
        this.connectionEstablished.emit(false);
    });

    this.connection.reconnecting(() => {
        this.tryingToReconnect = true;
    });

    this.connection.reconnected(() => {
        this.tryingToReconnect = false;
    });
    this.connection.error((error: any) => {
        this.initialize();
    });
    this.connection.disconnected(() => {
        if (this.tryingToReconnect) {
            setTimeout(() => {
                this.initialize();
            }, 5000);

        }
    });;   

由于上面的代码,signalr 会产生问题,例如 在以下情况下会导致客户端浏览器崩溃和内存泄漏,例如,

  1. 如果客户端互联网连接出现问题几个小时,那段时间 singalr 会不断尝试创建与集线器的连接。由于网络问题,在浏览器控制台中无限记录连接错误,直到与 hub 连接成功

  2. 如果我关闭服务器,那时间也会 singalR 在浏览器控制台中无限记录连接错误,直到与 hub 成功连接

如何解决signalr始终与Hub保持连接的问题?

【问题讨论】:

    标签: asp.net-mvc signalr


    【解决方案1】:

    我会创建一个增量值,并且每次重新连接超时都会增加,并显示一个立即重试选项,就像 Gmail 一样,是一种很好的做法,可以解决这两个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 2019-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多