From c067951d50fcf6cbb4464d8b837edd55022580b9 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: Mon, 28 Feb 2022 18:36:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9signalR=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ZR.Vue/src/App.vue | 16 ++++++++++++++++ ZR.Vue/src/utils/signalR.js | 14 +++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/ZR.Vue/src/App.vue b/ZR.Vue/src/App.vue index 907acd2..44ee14d 100644 --- a/ZR.Vue/src/App.vue +++ b/ZR.Vue/src/App.vue @@ -5,7 +5,23 @@ diff --git a/ZR.Vue/src/utils/signalR.js b/ZR.Vue/src/utils/signalR.js index c7185b3..b821db3 100644 --- a/ZR.Vue/src/utils/signalR.js +++ b/ZR.Vue/src/utils/signalR.js @@ -1,6 +1,7 @@ // 官方文档:https://docs.microsoft.com/zh-cn/aspnet/core/signalr/javascript-client?view=aspnetcore-6.0&viewFallbackFrom=aspnetcore-2.2&tabs=visual-studio import * as signalR from '@microsoft/signalr' import store from '../store' +import { getToken } from '@/utils/auth' export default { // signalR对象 @@ -10,21 +11,24 @@ export default { baseUrl: '', init(url) { const connection = new signalR.HubConnectionBuilder() - .withUrl(url) + .withUrl(url, { accessTokenFactory: () => getToken() }) .build(); - // console.log('conn', connection); this.SR = connection; // 断线重连 connection.onclose(async () => { - await this.SR.start(); + console.log('断开连接了') + await this.start(); }) + connection.onreconnected(() => { + console.log('断线重连') + }) connection.on("onlineNum", (data) => { - store.dispatch("socket/changeOnlineNum", data); + store.dispatch("socket/changeOnlineNum", data); }); // 启动 - this.start(); + // this.start(); }, async start() { var that = this;