Files
Yi.Admin/Yi.RuoYi.Vue3/src/utils/signalR.js

121 lines
3.4 KiB
JavaScript
Raw Normal View History

2023-02-04 18:06:42 +08:00
// 官方文档https://docs.microsoft.com/zh-cn/aspnet/core/signalr/javascript-client?view=aspnetcore-6.0&viewFallbackFrom=aspnetcore-2.2&tabs=visual-studio
2024-01-06 12:45:14 +08:00
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";
2023-02-04 18:06:42 +08:00
export default {
// signalR对象
SR: {},
// 失败连接重试次数
failNum: 4,
2023-12-11 09:55:12 +08:00
async init(url) {
2023-02-04 18:06:42 +08:00
const connection = new signalR.HubConnectionBuilder()
2024-01-06 12:45:14 +08:00
.withUrl(`${import.meta.env.VITE_APP_BASE_WS}/` + url, {
headers: {
Authorization: `Bearer ${getToken()}`,
},
accessTokenFactory: () => {
// 返回授权 token
return `${getToken()}`;
},
})
2023-12-11 09:55:12 +08:00
2024-01-06 12:45:14 +08:00
.withAutomaticReconnect() //自动重新连接
2023-02-04 18:06:42 +08:00
.configureLogging(signalR.LogLevel.Information)
.build();
2023-12-11 09:55:12 +08:00
2024-01-06 12:45:14 +08:00
console.log(connection, "connection");
2023-12-11 09:55:12 +08:00
2023-02-04 18:06:42 +08:00
this.SR = connection;
// 断线重连
connection.onclose(async () => {
2024-01-06 12:45:14 +08:00
console.log("断开连接了");
console.assert(
connection.state === signalR.HubConnectionState.Disconnected
);
2023-02-04 18:06:42 +08:00
// 建议用户重新刷新浏览器
await this.start();
2024-01-06 12:45:14 +08:00
});
2023-02-04 18:06:42 +08:00
connection.onreconnected(() => {
2024-01-06 12:45:14 +08:00
console.log("断线重新连接成功");
});
2023-02-04 18:06:42 +08:00
this.receiveMsg(connection);
// 启动
2023-12-11 09:55:12 +08:00
await this.start();
2023-02-04 18:06:42 +08:00
},
/**
* 调用 this.signalR.start().then(async () => { await this.SR.invoke("method")})
2024-01-06 12:45:14 +08:00
* @returns
2023-02-04 18:06:42 +08:00
*/
2023-12-11 09:55:12 +08:00
async close() {
try {
var that = this;
await this.SR.stop();
2024-01-06 12:45:14 +08:00
} catch {}
2023-12-11 09:55:12 +08:00
},
2023-02-04 18:06:42 +08:00
async start() {
var that = this;
try {
//使用async和await 或 promise的then 和catch 处理来自服务端的异常
2023-12-11 09:55:12 +08:00
console.log(this.SR, "执行连接");
2023-02-04 18:06:42 +08:00
await this.SR.start();
//console.assert(this.SR.state === signalR.HubConnectionState.Connected);
2023-09-24 23:41:26 +08:00
//console.log('signalR 连接成功了', this.SR.state);
2023-02-04 18:06:42 +08:00
return true;
} catch (error) {
that.failNum--;
2023-09-24 23:41:26 +08:00
//console.log(`失败重试剩余次数${that.failNum}`, error)
2023-02-04 18:06:42 +08:00
if (that.failNum > 0) {
setTimeout(async () => {
2024-01-06 12:45:14 +08:00
await this.SR.start();
2023-02-04 18:06:42 +08:00
}, 5000);
}
return false;
}
},
// 接收消息处理
receiveMsg(connection) {
connection.on("onlineNum", (data) => {
const socketStore = useSocketStore();
2024-01-06 12:45:14 +08:00
socketStore.setOnlineNum(data);
2023-02-04 18:06:42 +08:00
});
connection.on("forceOut", (msg) => {
2024-01-06 12:45:14 +08:00
useUserStore()
.logOut()
.then(() => {
alert(msg);
location.href = "/index";
});
2023-02-04 18:06:42 +08:00
});
// connection.on("onlineNum", (data) => {
// store.dispatch("socket/changeOnlineNum", data);
// });
// // 接收欢迎语
// connection.on("welcome", (data) => {
// console.log('welcome', data)
// Notification.info(data)
// });
// // 接收后台手动推送消息
// connection.on("receiveNotice", (title, data) => {
// Notification({
// type: 'info',
// title: title,
// message: data,
// dangerouslyUseHTMLString: true,
// duration: 0
// })
// })
// // 接收系统通知/公告
// connection.on("moreNotice", (data) => {
// if (data.code == 200) {
// store.dispatch("socket/getNoticeList", data.data);
// }
// })
2024-01-06 12:45:14 +08:00
},
};