林旭祥 vor 5 Monaten
Ursprung
Commit
258fc4b4d3
2 geänderte Dateien mit 22 neuen und 19 gelöschten Zeilen
  1. 15 12
      src/utils/handWs.ts
  2. 7 7
      src/utils/trainWs.ts

+ 15 - 12
src/utils/handWs.ts

@@ -3,11 +3,22 @@ import { ref, onMounted, onUnmounted } from 'vue';
 import useAppStore from '@/store/modules/app';
 import router from '@/router';
 
+const address: any = import.meta.env.VITE_APP_BASE_API;
+const token: any = localStorage.getItem('token');
+const deviceid: any = localStorage.getItem('deviceid');
+const myToken: any = 'JWT ' + token;
+
+let socketHand: any = null; //ws实例对象
+socketHand = io(address + '/', {
+  transports: ['websocket', 'polling'],
+  query: {
+    Authorization: token ? myToken : ''
+  }
+});
+
+
 export function useWebSocket() {
-  const address: any = import.meta.env.VITE_APP_BASE_API;
-  const token: any = localStorage.getItem('token');
-  const deviceid: any = localStorage.getItem('deviceid');
-  const myToken: any = 'JWT ' + token;
+
 
   // let socketHand: any = useAppStore().getSocketHand() || null; //ws实例对象
   // if (socketHand == null) {
@@ -20,14 +31,6 @@ export function useWebSocket() {
   //   useAppStore().setSocketHand(socketHand);
   // }
 
-  let socketHand: any = null; //ws实例对象
-  socketHand = io(address + '/', {
-    transports: ['websocket', 'polling'],
-    query: {
-      Authorization: token ? myToken : ''
-    }
-  });
-
   function handWs(callback: any) {
     if (socketHand == null) {
       return false;

+ 7 - 7
src/utils/trainWs.ts

@@ -1,10 +1,10 @@
 import io from 'socket.io-client';
 import { ref, onMounted, onUnmounted } from 'vue';
+const address: any = import.meta.env.VITE_APP_BASE_API;
+const token: any = localStorage.getItem('token');
+const myInfo: any = localStorage.getItem('userInfo');
 
 export function useWs() {
-  const address: any = import.meta.env.VITE_APP_BASE_API;
-  const token: any = localStorage.getItem('token');
-  const myInfo: any = localStorage.getItem('userInfo');
   let socket: any = null; //ws实例对象
   let timerManager: any = {}; //计时器管理
   let parameter: any = {}; //参数
@@ -602,10 +602,10 @@ export function useWs() {
   }
 
   onUnmounted(() => {
-    // if (socket) {
-    //   socket.close();
-    //   socket = null;
-    // }
+    if (socket) {
+      socket.close();
+      socket = null;
+    }
   });
 
   return {