断开连接后如何正确重新连接SignalR

时间:2018-12-08 13:29:28

标签: asp.net-mvc signalr

在我的ASP.NET服务器应用程序的angular6网站中, 我希望singnalR客户端应始终与Hub(Server)保持连接。为此,使用下面的代码捕捉在断开事件上开始连接。

    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);

        }
    });;   

由于上述代码,signaler产生了诸如 在以下情况下破坏客户端浏览器和内存泄漏

  1. 如果客户端Internet连接持续几个小时存在问题,则该时间持续尝试与集线器建立连接。由于互联网问题,无限次在浏览器控制台中记录连接错误,直到与集线器成功连接

  2. 如果我关闭服务器,那时候还会单独在浏览器控制台中无限记录连接错误,直到与集线器成功连接

    SignalRErroronConsole

如何纠正信号器始终与集线器保持连接的问题?

1 个答案:

答案 0 :(得分:1)

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

相关问题