信号还原连接



如果后端服务器出现故障,如何在不刷新客户端应用程序浏览器的情况下恢复连接,并在 5 分钟左右恢复

...

const connection = new signalR.HubConnectionBuilder()
.withUrl(`/place/${props.match.params.id}`)
.withAutomaticReconnect()
.build();
connection.onreconnecting(function() {
setError('connection lost..');
var reconnectionInterval = setInterval(() => {
if (connection.state == signalR.HubConnectionState.Disconnected) {
connection.stop();

try {
console.log('try to reconnect');
connection.start();
} catch (e) {
//doesn't go here, how to handle if the server still down
console.log('failed to reconnect');
}

}
}, 5000);
});

这似乎有效

if (connection.state == signalR.HubConnectionState.Disconnected) {
connection.start()
.then(x => {
clearInterval(reconnectionInterval);
setError(null);
})
.catch(ex => setError('still disconnected'));
}

最新更新