服务器重启时自动重新连接SignalR客户端

时间:2020-01-01 09:50:22

标签: angular asp.net-core aspnetboilerplate asp.net-core-signalr

我在ASP.NET Boilerplate项目中使用ASP.NET Core SignalR,服务器启动后一切正常。

但是由于任何我需要重新启动服务器的原因,我都会看到以下错误:

enter image description here

我必须刷新网页才能重新连接到SignalR。

有什么方法可以检查服务器并重新连接而不刷新吗?

我使用Angular模板和ABP v4.0.1随附的默认SignalR客户端。

1 个答案:

答案 0 :(得分:1)

此问题已在模板的v5.1.1中修复:aspnetboilerplate/module-zero-core-template#498

重新连接循环

yarn upgrade abp-web-resources@^4.1.0

在abp-web-resources@4.1.0中提供了重新连接循环:

// Reconnect loop
function start() {
    connection.start().catch(function () {
        setTimeout(function () {
            start();
        }, 5000);
    });
}

// Reconnect if hub disconnects
connection.onclose(function (e) {
    if (e) {
        abp.log.debug('Connection closed with error: ' + e);
    } else {
        abp.log.debug('Disconnected');
    }

    // if (!abp.signalr.autoConnect) {
    if (!abp.signalr.autoReconnect) {
        return;
    }

    // setTimeout(function () {
    //     connection.start();
    // }, 5000);
    start();
});

参考:

重新连接回路+断路器

yarn upgrade abp-web-resources@^5.1.1

断路器已在abp-web-resources@5.1.1中提供:

// Reconnect loop
function tryReconnect() {
    if (tries > abp.signalr.maxTries) {
        return;
    } else {
        connection.start()
            .then(() => {
                reconnectTime = abp.signalr.reconnectTime;
                tries = 1;
                console.log('Reconnected to SignalR server!');
            }).catch(() => {
                tries += 1;
                reconnectTime *= 2;
                setTimeout(() => tryReconnect(), reconnectTime);
            });
    }
}

// Reconnect if hub disconnects
connection.onclose(function (e) {
    if (e) {
        abp.log.debug('Connection closed with error: ' + e);
    } else {
        abp.log.debug('Disconnected');
    }

    if (!abp.signalr.autoReconnect) {
        return;
    }

    // start();
    tryReconnect();
});

参考: