¶Ô±ÈÐÂÎļþ |
| | |
| | | // 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("LocationData", (data) => { |
| | | eventBus.emit("locationData", data); |
| | | }); |
| | | }; |
| | | |
| | | //页é¢éæ¯ |
| | | // onUnmounted(() => {}) |
| | | return { startSignalr }; |
| | | } |