肖洋
2024-12-11 334a54bbc1300ef0556732b2ea624291a120d07b
添加SignalR及事件总线支持
已添加5个文件
108 ■■■■■ 文件已修改
Code Management/.vs/Code Management/v17/.wsuo 补丁 | 查看 | 原始文档 | blame | 历史
Code Management/.vs/Code Management/v17/workspaceFileList.bin 补丁 | 查看 | 原始文档 | blame | 历史
Code Management/.vs/slnx.sqlite 补丁 | 查看 | 原始文档 | blame | 历史
Code Management/WCS/WIDESEAWCS_Client/src/uitils/eventBus.js 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Code Management/WCS/WIDESEAWCS_Client/src/uitils/signalr.js 103 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Code Management/.vs/Code Management/v17/.wsuo
Binary files differ
Code Management/.vs/Code Management/v17/workspaceFileList.bin
Binary files differ
Code Management/.vs/slnx.sqlite
Binary files differ
Code Management/WCS/WIDESEAWCS_Client/src/uitils/eventBus.js
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,5 @@
import mitt from 'mitt';
const eventBus = mitt();
export default eventBus;
Code Management/WCS/WIDESEAWCS_Client/src/uitils/signalr.js
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,103 @@
// 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 };
}