feat:对接推送在线人数

This commit is contained in:
Xwen
2024-01-06 12:45:14 +08:00
parent 0bcc604757
commit ebad623032
2 changed files with 55 additions and 50 deletions

View File

@@ -1,9 +1,9 @@
// 官方文档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 useSocketStore from '@/store/modules/socket'
import { getToken } from '@/utils/auth'
import useUserStore from '@/store/modules/user'
import { ElMessage } from 'element-plus'
import * as signalR from "@microsoft/signalr";
import useSocketStore from "@/store/modules/socket";
import { getToken } from "@/utils/auth";
import useUserStore from "@/store/modules/user";
import { ElMessage } from "element-plus";
export default {
// signalR对象
@@ -12,57 +12,51 @@ export default {
failNum: 4,
async init(url) {
const connection = new signalR.HubConnectionBuilder()
.withUrl(`${import.meta.env.VITE_APP_BASE_WS}/` + url,
{
headers: {
'Authorization': `Bearer ${getToken()}`
},
accessTokenFactory: () => {
// 返回授权 token
return `${getToken()}`;
}
}
)
.withUrl(`${import.meta.env.VITE_APP_BASE_WS}/` + url, {
headers: {
Authorization: `Bearer ${getToken()}`,
},
accessTokenFactory: () => {
// 返回授权 token
return `${getToken()}`;
},
})
.withAutomaticReconnect()//自动重新连接
.withAutomaticReconnect() //自动重新连接
.configureLogging(signalR.LogLevel.Information)
.build();
console.log(connection, "connection")
console.log(connection, "connection");
this.SR = connection;
// 断线重连
connection.onclose(async () => {
console.log('断开连接了')
console.assert(connection.state === signalR.HubConnectionState.Disconnected);
console.log("断开连接了");
console.assert(
connection.state === signalR.HubConnectionState.Disconnected
);
// 建议用户重新刷新浏览器
await this.start();
})
});
connection.onreconnected(() => {
console.log('断线重新连接成功')
})
console.log("断线重新连接成功");
});
this.receiveMsg(connection);
// 启动
await this.start();
},
/**
* 调用 this.signalR.start().then(async () => { await this.SR.invoke("method")})
* @returns
* @returns
*/
async close() {
try {
var that = this;
await this.SR.stop();
}
catch
{
}
} catch {}
},
async start() {
var that = this;
@@ -78,7 +72,7 @@ export default {
//console.log(`失败重试剩余次数${that.failNum}`, error)
if (that.failNum > 0) {
setTimeout(async () => {
await this.SR.start()
await this.SR.start();
}, 5000);
}
return false;
@@ -88,13 +82,15 @@ export default {
receiveMsg(connection) {
connection.on("onlineNum", (data) => {
const socketStore = useSocketStore();
socketStore.setOnlineNum(data)
socketStore.setOnlineNum(data);
});
connection.on("forceOut", (msg) => {
useUserStore().logOut().then(() => {
alert(msg);
location.href = '/index';
})
useUserStore()
.logOut()
.then(() => {
alert(msg);
location.href = "/index";
});
});
// connection.on("onlineNum", (data) => {
// store.dispatch("socket/changeOnlineNum", data);
@@ -120,5 +116,5 @@ export default {
// store.dispatch("socket/getNoticeList", data.data);
// }
// })
}
}
},
};