我正在尝试检测SignalR Core连接何时丢失,以便我可以创建一个新的连接或者至少警告用户。
connection.on('closed', data => {
alert('Connection Closed');
});
这似乎没有任何效果。消息停止到达,但是此处理程序未被触发。
另外,关于事件处理的文档在这个库的新版本中在哪里可以找到?
我正在尝试检测SignalR Core连接何时丢失,以便我可以创建一个新的连接或者至少警告用户。
connection.on('closed', data => {
alert('Connection Closed');
});
这似乎没有任何效果。消息停止到达,但是此处理程序未被触发。
另外,关于事件处理的文档在这个库的新版本中在哪里可以找到?
使用 onclose
:
connection.onclose(function (e) {
alert('Connection Closed');
}
目前还没有文档,但在 GitHub 上有一些示例。
const connection = new signalR.HubConnectionBuilder()
.withUrl("/chathub")
.configureLogging(signalR.LogLevel.Information)
.build();
async function start() {
try {
await connection.start();
console.log("SignalR Connected.");
} catch (err) {
console.log(err);
setTimeout(start, 5000);
}
};
connection.onclose(async () => {
await start();
});
// Start the connection.
start();