Sfoglia il codice sorgente

定时重连websocket

Michael Wang 3 anni fa
parent
commit
501fd88803
1 ha cambiato i file con 29 aggiunte e 1 eliminazioni
  1. 29 1
      src/features/OnlineExam/Examing/ws.js

+ 29 - 1
src/features/OnlineExam/Examing/ws.js

@@ -19,6 +19,34 @@ export function openWS({ examRecordDataId }) {
     action: "准备连接",
     examRecordDataId,
   });
+  if (ws && (ws.readyState === 0 || ws.readyState === 1)) {
+    createLog({
+      type: "微信小程序websocket",
+      action: "准备连接-旧websocket状态已经是开启或正在连接状态",
+      readyState: ws.readyState,
+      examRecordDataId,
+    });
+    return;
+  }
+  if (ws && ws.readyState !== 3) {
+    createLog({
+      type: "微信小程序websocket",
+      action: "准备连接-旧websocket状态不是关闭",
+      readyState: ws.readyState,
+      examRecordDataId,
+    });
+
+    try {
+      ws.close();
+    } catch (e) {
+      createLog({
+        type: "微信小程序websocket",
+        action: "关闭ws异常。",
+        dtl: "再次关闭",
+        detail: e,
+      });
+    }
+  }
   try {
     ws = new WebSocket(
       VUE_APP_WK_SERVER_SOCKET_FOR_AUDIO +
@@ -100,7 +128,7 @@ export function openWS({ examRecordDataId }) {
           action: "连接被关闭后-客户端不允许关闭-准备连接",
           detail: "onclose",
         });
-        openWS({ examRecordDataId });
+        setTimeout(() => openWS({ examRecordDataId }), 5 * 1000);
       }
     };