Forráskód Böngészése

websocket 关闭多个心跳的 interval

Michael Wang 5 éve
szülő
commit
9d2b8b69c5

+ 1 - 1
src/features/OnlineExam/Examing/TextQuestionView.vue

@@ -374,7 +374,7 @@ export default {
         ele.innerHTML = this.studentAnswer || "";
         const pEle = ele.querySelectorAll(".photo-answers-block");
         if (pEle) [...pEle].forEach(v => v.remove());
-        console.log(ele.innerHTML);
+        // console.log(ele.innerHTML);
 
         if (!ele.innerHTML && pSrcs.length === 0) {
           // 完全为空则重置答案

+ 8 - 3
src/features/OnlineExam/Examing/ws.js

@@ -4,7 +4,7 @@ import { VUE_APP_WK_SERVER_SOCKET_FOR_AUDIO } from "@/constants/constants";
 
 let ws;
 let shouldReconnect = true;
-let heartbeatId = null;
+let heartbeatIds = [];
 const RECONNECT_INTERVAL = 3000;
 const HEARTBEAT_INTERVAL = 50 * 1000;
 let reconnectNumber = 0;
@@ -25,7 +25,10 @@ export function openWS({ examRecordDataId }) {
 
     ws.onclose = () => {
       console.log("ws close by server");
-      clearInterval(heartbeatId);
+      for (const heartbeatId of heartbeatIds) {
+        clearInterval(heartbeatId);
+      }
+      heartbeatIds = [];
       if (shouldReconnect) {
         console.log("close -> reconnect");
         setTimeout(() => {
@@ -82,13 +85,15 @@ export function openWS({ examRecordDataId }) {
 }
 
 function heartbeat() {
-  heartbeatId = setInterval(() => {
+  const heartbeatId = setInterval(() => {
     ws.send(
       JSON.stringify({
         eventType: "HEARTBEAT",
       })
     );
   }, HEARTBEAT_INTERVAL);
+
+  heartbeatIds.push(heartbeatId);
 }
 
 export function closeWsWithoutReconnect() {