林旭祥 6 месяцев назад
Родитель
Сommit
7e772d68ff
2 измененных файлов с 14 добавлено и 17 удалено
  1. 12 15
      src/utils/handWs.ts
  2. 2 2
      src/utils/trainWs.ts

+ 12 - 15
src/utils/handWs.ts

@@ -1,10 +1,8 @@
 import io from 'socket.io-client';
-import { ref, onMounted, onUnmounted } from 'vue';
+import { ref, onMounted, onUnmounted, onBeforeUnmount } from 'vue';
 import useAppStore from '@/store/modules/app';
 // import router from '@/router';
 
-
-
 // let socketHand: any = null; //ws实例对象
 // socketHand = io(address + '/', {
 //   transports: ['websocket', 'polling'],
@@ -14,7 +12,6 @@ import useAppStore from '@/store/modules/app';
 //   }
 // });
 
-
 export function useWebSocket() {
   const address: any = import.meta.env.VITE_APP_BASE_API;
   const token: any = localStorage.getItem('token');
@@ -80,7 +77,7 @@ export function useWebSocket() {
       return false;
     }
     if (socketHand.connected) {
-      callback = callback || function () { };
+      callback = callback || function () {};
       socketHand.emit(type, data, callback);
     }
   }
@@ -94,7 +91,7 @@ export function useWebSocket() {
       {
         deviceid: deviceid ? deviceid : ''
       },
-      () => { }
+      () => {}
     );
   }
 
@@ -107,7 +104,7 @@ export function useWebSocket() {
       {
         data: data
       },
-      () => { }
+      () => {}
     );
   }
 
@@ -121,7 +118,7 @@ export function useWebSocket() {
         hctrl_name: `handcontroller_${data}`,
         cmd: 'open_handcontroller'
       },
-      () => { }
+      () => {}
     );
   }
 
@@ -135,7 +132,7 @@ export function useWebSocket() {
         hctrl_name: `handcontroller_${data}`,
         cmd: 'get_handcontroller_state'
       },
-      () => { }
+      () => {}
     );
   }
 
@@ -149,11 +146,11 @@ export function useWebSocket() {
         hctrl_name: `handcontroller_${data}`,
         cmd: 'terminate_handcontroller'
       },
-      () => { }
+      () => {}
     );
   }
 
-  onUnmounted(() => {
+  onBeforeUnmount(() => {
     // if (socketHand) {
     //   // if(['/gesture'].includes(router.currentRoute.value.path)){
     //   //   socketHand.close();
@@ -162,10 +159,10 @@ export function useWebSocket() {
     //   socketHand = null;
     // }
     // getClearTimer();
-    // if (socketHand) {
-    //   socketHand.close();
-    //   socketHand = null;
-    // }
+    if (socketHand) {
+      socketHand.close();
+      socketHand = null;
+    }
   });
 
   return { handWs, sendMessage, startLogin, startDevice, startHand, stateHand, closeHand };

+ 2 - 2
src/utils/trainWs.ts

@@ -1,5 +1,5 @@
 import io from 'socket.io-client';
-import { ref, onMounted, onUnmounted } from 'vue';
+import { ref, onMounted, onUnmounted, onBeforeUnmount } from 'vue';
 
 export function useWs() {
   const address: any = import.meta.env.VITE_APP_BASE_API;
@@ -639,7 +639,7 @@ export function useWs() {
     return new Promise((resolve) => setTimeout(resolve, ms));
   }
 
-  onUnmounted(() => {
+  onBeforeUnmount(() => {
     getClearTimer();
     if (socket) {
       socket.close();