From 334a54bbc1300ef0556732b2ea624291a120d07b Mon Sep 17 00:00:00 2001
From: 肖洋 <cathay_xy@163.com>
Date: 星期三, 11 十二月 2024 14:52:18 +0800
Subject: [PATCH] 添加SignalR及事件总线支持
---
Code Management/.vs/Code Management/v17/workspaceFileList.bin | 0
Code Management/WCS/WIDESEAWCS_Client/src/uitils/signalr.js | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++
Code Management/.vs/slnx.sqlite | 0
Code Management/WCS/WIDESEAWCS_Client/src/uitils/eventBus.js | 5 ++
Code Management/.vs/Code Management/v17/.wsuo | 0
5 files changed, 108 insertions(+), 0 deletions(-)
diff --git a/Code Management/.vs/Code Management/v17/.wsuo b/Code Management/.vs/Code Management/v17/.wsuo
new file mode 100644
index 0000000..77c39f3
--- /dev/null
+++ b/Code Management/.vs/Code Management/v17/.wsuo
Binary files differ
diff --git a/Code Management/.vs/Code Management/v17/workspaceFileList.bin b/Code Management/.vs/Code Management/v17/workspaceFileList.bin
new file mode 100644
index 0000000..66fb339
--- /dev/null
+++ b/Code Management/.vs/Code Management/v17/workspaceFileList.bin
Binary files differ
diff --git a/Code Management/.vs/slnx.sqlite b/Code Management/.vs/slnx.sqlite
new file mode 100644
index 0000000..9eff72b
--- /dev/null
+++ b/Code Management/.vs/slnx.sqlite
Binary files differ
diff --git a/Code Management/WCS/WIDESEAWCS_Client/src/uitils/eventBus.js b/Code Management/WCS/WIDESEAWCS_Client/src/uitils/eventBus.js
new file mode 100644
index 0000000..3f6a58d
--- /dev/null
+++ b/Code Management/WCS/WIDESEAWCS_Client/src/uitils/eventBus.js
@@ -0,0 +1,5 @@
+import mitt from 'mitt';
+
+const eventBus = mitt();
+
+export default eventBus;
diff --git a/Code Management/WCS/WIDESEAWCS_Client/src/uitils/signalr.js b/Code Management/WCS/WIDESEAWCS_Client/src/uitils/signalr.js
new file mode 100644
index 0000000..e6b1948
--- /dev/null
+++ b/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; // 淇濆瓨褰撳墠鎵撳紑鐨凬otification瀹炰緥
+ let socketUrl = "hubs/simple"; //socket鍦板潃
+ // if (sysConfig.VITE_PROXY === 'false') {
+ socketUrl = http.ipAddress + socketUrl; //鍒ゆ柇鏄惁瑕佽蛋浠g悊妯″紡锛岃蛋浜嗙殑璇濆彂甯冧箣鍚庣洿鎺ginx浠g悊
+ // }
+ //寮�濮�
+ const startSignalr = () => {
+ //鍒濆鍖栬繛鎺�
+ const connection = init();
+ // 鍚姩杩炴帴
+ connection
+ .start()
+ .then(() => {
+ console.log("鍚姩杩炴帴");
+ })
+ .catch((err) => {
+ console.log("杩炴帴澶辫触", err);
+ });
+ };
+
+ //鍒濆鍖�
+ const init = () => {
+ console.log("鍒濆鍖朣ignalR瀵硅薄");
+ // 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) {
+ // 涓婁竴涓狽otification宸插叧闂垨灏氭湭鎵撳紑
+ openedNotification = ElNotification({
+ title: "鎴愬姛",
+ message: data,
+ type: "success",
+ onClose: () => {
+ // Notification宸插叧闂�
+ openedNotification = null; // 娓呯┖褰撳墠鎵撳紑鐨凬otification瀹炰緥
+ 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 };
+}
--
Gitblit v1.9.3