navbar新增消息通知

This commit is contained in:
不做码农
2022-04-25 18:35:28 +08:00
parent bf625f2ca6
commit 4ad53af42d
6 changed files with 154 additions and 21 deletions
+16 -15
View File
@@ -10,26 +10,27 @@ export default {
// 失败连接重试次数
failNum: 4,
baseUrl: '',
init(url) {
console.log(url);
init(url) {
var socketUrl = window.location.origin + url
//console.log(socketUrl)
const connection = new signalR.HubConnectionBuilder()
.withUrl(url, { accessTokenFactory: () => getToken() })
.withAutomaticReconnect()//自动重新连接
.withUrl(socketUrl, { accessTokenFactory: () => getToken() })
.withAutomaticReconnect() //自动重新连接
.configureLogging(signalR.LogLevel.Information)
.build();
this.SR = connection;
this.SR = connection
// 断线重连
connection.onclose(async () => {
console.log('断开连接了')
console.assert(connection.state === signalR.HubConnectionState.Disconnected);
// 建议用户重新刷新浏览器
await this.start();
console.assert(connection.state === signalR.HubConnectionState.Disconnected);
// 建议用户重新刷新浏览器
await this.start()
})
connection.onreconnected(() => {
console.log('断线重新连接成功')
})
this.receiveMsg(connection);
this.receiveMsg(connection)
// 启动
// this.start();
},
@@ -38,26 +39,26 @@ export default {
* @returns
*/
async start() {
var that = this;
var that = this
try {
//使用async和await 或 promise的then 和catch 处理来自服务端的异常
await this.SR.start();
await this.SR.start()
//console.assert(this.SR.state === signalR.HubConnectionState.Connected);
console.log('signalR 连接成功了', this.SR.state);
return true;
return true
} catch (error) {
that.failNum--;
console.log(`失败重试剩余次数${that.failNum}`, error)
if (that.failNum > 0) {
setTimeout(async () => {
await this.SR.start()
}, 5000);
}, 5000)
}
return false;
return false
}
},
// 接收消息处理
// 接收消息处理
receiveMsg(connection) {
connection.on("onlineNum", (data) => {
store.dispatch("socket/changeOnlineNum", data);