// import sysConfig from '@/config/index' // import tool from '@/utils/tool' import store from "../store/index"; import http from "@/../src/api/http.js"; import * as signalR from "@microsoft/signalr"; import { ElNotification } from "element-plus"; import eventBus from "./eventBus"; // import * as signalrMessage from './mqtt/message' //使用signalr export default function useSignalr() { // const userInfo = tool.data.get('USER_INFO') //ç”¨æˆ·ä¿¡æ¯ let openedNotification = null; // ä¿å˜å½“剿‰“开的Notification实例 let socketUrl = "hubs/simple"; //socketåœ°å€ // if (sysConfig.VITE_PROXY === 'false') { socketUrl = http.ipAddress + socketUrl; //åˆ¤æ–æ˜¯å¦è¦èµ°ä»£ç†æ¨¡å¼ï¼Œèµ°äº†çš„è¯å‘布之åŽç›´æŽ¥nginxä»£ç† // } //开始 const startSignalr = () => { //åˆå§‹åŒ–连接 const connection = init(); // å¯åŠ¨è¿žæŽ¥ connection .start() .then(() => { console.log("å¯åŠ¨è¿žæŽ¥"); }) .catch((err) => { console.log("连接失败", err); }); }; //åˆå§‹åŒ– const init = () => { console.log("åˆå§‹åŒ–SignalR对象"); // SignalR对象 const connection = new signalR.HubConnectionBuilder() .withUrl(socketUrl, { accessTokenFactory: () => store.getters.getToken(), skipNegotiation: true, transport: signalR.HttpTransportType.WebSockets, }) .withAutomaticReconnect({ nextRetryDelayInMilliseconds: () => { return 5000; // æ¯5ç§’é‡è¿žä¸€æ¬¡ }, }) //è‡ªåŠ¨é‡æ–°è¿žæŽ¥ .configureLogging(signalR.LogLevel.Information) .build(); connection.keepAliveIntervalInMilliseconds = 15 * 1000; // 心跳检测15s // connection.serverTimeoutInMilliseconds = 30 * 60 * 1000 // è¶…æ—¶æ—¶é—´30m // æ–开连接 connection.onclose(async () => { console.log("æ–开连接"); }); //æ–çº¿é‡æ–° connection.onreconnected(() => { console.log("æ–çº¿é‡æ–°è¿žæŽ¥æˆåŠŸ"); }); //消æ¯å¤„ç† receiveMsg(connection); return connection; }; //接收消æ¯å¤„ç† const receiveMsg = (connection) => { //接收登出 connection.on("LoginOut", (data) => { // signalrMessage.loginOut(data) }); connection.on("NewMessage", (data) => { eventBus.emit("stackerDataError", data); if (openedNotification === null || openedNotification.closed) { // 上一个Notification已关闿ˆ–尚未打开 openedNotification = ElNotification({ title: "æˆåŠŸ", message: data, type: "success", onClose: () => { // Notificationå·²å…³é— openedNotification = null; // æ¸…ç©ºå½“å‰æ‰“开的Notification实例 console.log("Notification已关é—"); }, }); } }); connection.on("StackerData", (data) => { // console.log(data); eventBus.emit("stackerData", data); }); connection.on("LineData", (data) => { eventBus.emit("locationData", data); }); connection.on("Logs", (data) => { eventBus.emit("Logs", data); }); }; //页é¢é”€æ¯ // onUnmounted(() => {}) return { startSignalr }; }