新增signalR自动重连

This commit is contained in:
不做码农 2022-03-07 12:27:53 +08:00
parent 77c57dc459
commit 6a3547dad2

View File

@ -13,16 +13,19 @@ export default {
init(url) { init(url) {
const connection = new signalR.HubConnectionBuilder() const connection = new signalR.HubConnectionBuilder()
.withUrl(url, { accessTokenFactory: () => getToken() }) .withUrl(url, { accessTokenFactory: () => getToken() })
.withAutomaticReconnect()//自动重新连接
.build(); .build();
this.SR = connection; this.SR = connection;
// 断线重连 // 断线重连
connection.onclose(async () => { connection.onclose(async () => {
console.log('断开连接了') console.log('断开连接了')
console.assert(connection.state === signalR.HubConnectionState.Disconnected);
// 建议用户重新刷新浏览器
await this.start(); await this.start();
}) })
connection.onreconnected(() => { connection.onreconnected(() => {
console.log('断线重连') console.log('断线重接成功')
}) })
this.receiveMsg(connection); this.receiveMsg(connection);
// 启动 // 启动