林旭祥 3 weeks ago
parent
commit
27a61ffcc1
4 changed files with 80 additions and 39 deletions
  1. 11 0
      package-lock.json
  2. 1 0
      package.json
  3. 1 0
      src/types/declareModule.ts
  4. 67 39
      src/utils/trainWs.ts

+ 11 - 0
package-lock.json

@@ -20,6 +20,7 @@
         "gsap": "^3.12.5",
         "phaser": "^3.88.2",
         "pinia": "^2.1.7",
+        "pubsub-js": "^1.9.5",
         "qrcode": "^1.5.4",
         "socket.io-client": "^2.5.0",
         "speak-tts": "^2.0.8",
@@ -5507,6 +5508,11 @@
       "resolved": "https://registry.npmmirror.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
       "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
     },
+    "node_modules/pubsub-js": {
+      "version": "1.9.5",
+      "resolved": "https://registry.npmjs.org/pubsub-js/-/pubsub-js-1.9.5.tgz",
+      "integrity": "sha512-5MZ0I9i5JWVO7SizvOviKvZU2qaBbl2KQX150FAA+fJBwYpwOUId7aNygURWSdPzlsA/xZ/InUKXqBbzM0czTA=="
+    },
     "node_modules/punycode": {
       "version": "2.3.1",
       "resolved": "https://registry.npmmirror.com/punycode/-/punycode-2.3.1.tgz",
@@ -11782,6 +11788,11 @@
       "resolved": "https://registry.npmmirror.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
       "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
     },
+    "pubsub-js": {
+      "version": "1.9.5",
+      "resolved": "https://registry.npmjs.org/pubsub-js/-/pubsub-js-1.9.5.tgz",
+      "integrity": "sha512-5MZ0I9i5JWVO7SizvOviKvZU2qaBbl2KQX150FAA+fJBwYpwOUId7aNygURWSdPzlsA/xZ/InUKXqBbzM0czTA=="
+    },
     "punycode": {
       "version": "2.3.1",
       "resolved": "https://registry.npmmirror.com/punycode/-/punycode-2.3.1.tgz",

+ 1 - 0
package.json

@@ -24,6 +24,7 @@
     "gsap": "^3.12.5",
     "phaser": "^3.88.2",
     "pinia": "^2.1.7",
+    "pubsub-js": "^1.9.5",
     "qrcode": "^1.5.4",
     "socket.io-client": "^2.5.0",
     "speak-tts": "^2.0.8",

+ 1 - 0
src/types/declareModule.ts

@@ -2,3 +2,4 @@ declare module 'socket.io-client';
 declare module 'speak-tts';
 declare module 'qrcode';
 declare module 'crypto-js';
+declare module 'pubsub-js';

+ 67 - 39
src/utils/trainWs.ts

@@ -1,4 +1,5 @@
 import io from 'socket.io-client';
+import PubSub from 'pubsub-js';
 import { ref, onMounted, onUnmounted, onBeforeUnmount } from 'vue';
 
 export function useWs() {
@@ -119,6 +120,9 @@ export function useWs() {
 
     socket.on('msg2frontend', (e: any) => {
       callback(e);
+			if (['init_result', 'set_exam_state', 'disconnect_request'].includes(e.cmd)) {
+				console.log(`【接收:${e.cmd}】`, JSON.stringify(e));
+			}
       //实时状态
       if (e.cmd === 'exam_status') {
         let index = examStateList.findIndex((item: any) => {
@@ -180,6 +184,7 @@ export function useWs() {
       }
       //状态变更
       if (e.cmd === 'set_exam_state') {
+        
         // getClearExamStateTimer();
         let index = examStateList.findIndex((item: any) => {
           return item.examId == e.exam_id;
@@ -187,6 +192,7 @@ export function useWs() {
         if (index != -1) {
           examStateList[index].examState = e.data;
           examStateList[index].wsStatus = e.status;
+          PubSub.publish(e.exam_id, e);
         }
         if (e.data == 3) {
           //关闭遮罩层
@@ -311,14 +317,18 @@ export function useWs() {
           exam_id: examId
         }
       });
-      examStateList[index].timer1 = setInterval(() => {
-        console.log(`【${examId}】1111111111111`)
-        if (examStateList[index].examState != 3) {
-          clearInterval(examStateList[index].timer1);
-          // clearTimeout(timer2);
+      PubSub.subscribe(examId, (eventName:any,res:any)=>{
+        PubSub.unsubscribe(examId);
           resolve({ data: examStateList[index].examState });
-        }
-      }, 250);
+      });
+      // examStateList[index].timer1 = setInterval(() => {
+      //   console.log(`【${examId}】1111111111111`)
+      //   if (examStateList[index].examState != 3) {
+      //     clearInterval(examStateList[index].timer1);
+      //     // clearTimeout(timer2);
+      //     resolve({ data: examStateList[index].examState });
+      //   }
+      // }, 250);
       // let timer2 = setTimeout(() => {
       //   if (examStateList[index].examState == 3) {
       //     clearInterval(timer1);
@@ -344,14 +354,18 @@ export function useWs() {
           exam_id: examId
         }
       });
-      examStateList[index].timer2 = setInterval(() => {
-        console.log(`【${examId}】2222222222222`)
-        if (examStateList[index].examState != 40) {
-          clearInterval(examStateList[index].timer2);
-          // clearTimeout(timer2);
-          resolve({ data: examStateList[index].examState });
-        }
-      }, 250);
+      PubSub.subscribe(examId, (eventName:any,res:any)=>{
+        PubSub.unsubscribe(examId);
+        resolve({ data: examStateList[index].examState });
+      });
+      // examStateList[index].timer2 = setInterval(() => {
+      //   console.log(`【${examId}】2222222222222`)
+      //   if (examStateList[index].examState != 40) {
+      //     clearInterval(examStateList[index].timer2);
+      //     // clearTimeout(timer2);
+      //     resolve({ data: examStateList[index].examState });
+      //   }
+      // }, 250);
       // let timer2 = setTimeout(() => {
       //   if (examStateList[index].examState == 40) {
       //     clearInterval(timer1);
@@ -377,14 +391,18 @@ export function useWs() {
           exam_id: examId
         }
       });
-      examStateList[index].timer3 = setInterval(() => {
-        console.log(`【${examId}】3333333333333`)
-        if (examStateList[index].examState != 41) {
-          clearInterval(examStateList[index].timer3);
-          // clearTimeout(timer2);
-          resolve({ data: examStateList[index].examState });
-        }
-      }, 250);
+      PubSub.subscribe(examId, (eventName:any,res:any)=>{
+        PubSub.unsubscribe(examId);
+        resolve({ data: examStateList[index].examState });
+      });
+      // examStateList[index].timer3 = setInterval(() => {
+      //   console.log(`【${examId}】3333333333333`)
+      //   if (examStateList[index].examState != 41) {
+      //     clearInterval(examStateList[index].timer3);
+      //     // clearTimeout(timer2);
+      //     resolve({ data: examStateList[index].examState });
+      //   }
+      // }, 250);
       // let timer2 = setTimeout(() => {
       //   if (examStateList[index].examState == 41) {
       //     clearInterval(timer1);
@@ -462,14 +480,18 @@ export function useWs() {
           exam_id: examId
         }
       });
-      examStateList[index].timer4 = setInterval(() => {
-        console.log(`【${examId}】4444444444444`)
-        if (examStateList[index].examState != 42) {
-          clearInterval(examStateList[index].timer4);
-          // clearTimeout(timer2);
-          resolve({ data: examStateList[index].examState });
-        }
-      }, 250);
+      PubSub.subscribe(examId, (eventName:any,res:any)=>{
+        PubSub.unsubscribe(examId);
+        resolve({ data: examStateList[index].examState });
+      });
+      // examStateList[index].timer4 = setInterval(() => {
+      //   console.log(`【${examId}】4444444444444`)
+      //   if (examStateList[index].examState != 42) {
+      //     clearInterval(examStateList[index].timer4);
+      //     // clearTimeout(timer2);
+      //     resolve({ data: examStateList[index].examState });
+      //   }
+      // }, 250);
       // let timer2 = setTimeout(() => {
       //   if (examStateList[index].examState == 42) {
       //     clearInterval(timer1);
@@ -495,14 +517,18 @@ export function useWs() {
           exam_id: examId
         }
       });
-      examStateList[index].timer5 = setInterval(() => {
-        console.log(`【${examId}】5555555555555`)
-        if (examStateList[index].examState >= 3) {
-          clearInterval(examStateList[index].timer5);
-          // clearTimeout(timer2);
-          resolve({ data: examStateList[index].examState });
-        }
-      }, 250);
+      PubSub.subscribe(examId, (eventName:any,res:any)=>{
+        PubSub.unsubscribe(examId);
+        resolve({ data: examStateList[index].examState });
+      });
+      // examStateList[index].timer5 = setInterval(() => {
+      //   console.log(`【${examId}】5555555555555`)
+      //   if (examStateList[index].examState >= 3) {
+      //     clearInterval(examStateList[index].timer5);
+      //     // clearTimeout(timer2);
+      //     resolve({ data: examStateList[index].examState });
+      //   }
+      // }, 250);
       // let timer2 = setTimeout(() => {
       //   if (examStateList[index].examState != 3) {
       //     clearInterval(timer1);
@@ -598,6 +624,8 @@ export function useWs() {
     loading?.close();
     //清除计时器
     getClearTimer();
+    //取消订阅
+    // PubSub.unsubscribe('setExamState');
     //通知工作站关闭
     if (testList.length > 1) {
       //单WS多区