From 075bdb8667f65922a5e58e8596569024df595352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Wed, 9 Mar 2022 21:30:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96signalR=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ZR.Vue/src/store/modules/socket.js | 2 +- ZR.Vue/src/utils/signalR.js | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/ZR.Vue/src/store/modules/socket.js b/ZR.Vue/src/store/modules/socket.js index a7c68f2..5d9935b 100644 --- a/ZR.Vue/src/store/modules/socket.js +++ b/ZR.Vue/src/store/modules/socket.js @@ -16,7 +16,7 @@ const actions = { changeOnlineNum({ commit }, data) { commit('SET_ONLINEUSER_NUM', data) }, - // 更新系统通知 + // 更新系统通知 getNoticeList({ commit }, data) { commit('SET_NOTICE_list', data) } diff --git a/ZR.Vue/src/utils/signalR.js b/ZR.Vue/src/utils/signalR.js index 13631e9..37f326e 100644 --- a/ZR.Vue/src/utils/signalR.js +++ b/ZR.Vue/src/utils/signalR.js @@ -13,6 +13,8 @@ export default { init(url) { const connection = new signalR.HubConnectionBuilder() .withUrl(url, { accessTokenFactory: () => getToken() }) + .withAutomaticReconnect()//自动重新连接 + .configureLogging(signalR.LogLevel.Information) .build(); this.SR = connection; // 断线重连 @@ -28,16 +30,19 @@ export default { // 启动 // this.start(); }, + /** + * 调用 this.signalR.start().then(async () => { await this.SR.invoke("method")}) + * @returns + */ async start() { var that = this; try { //使用async和await 或 promise的then 和catch 处理来自服务端的异常 - await this.SR.start(); - //console.assert(this.SR.state === signalR.HubConnectionState.Connected); console.log('signalR 连接成功了', this.SR.state); + return true; } catch (error) { that.failNum--; console.log(`失败重试剩余次数${that.failNum}`, error) @@ -46,6 +51,7 @@ export default { await this.SR.start() }, 5000); } + return false; } }, // 接收消息处理