Michael Wang пре 6 година
родитељ
комит
07d5c3c25b
1 измењених фајлова са 20 додато и 22 уклоњено
  1. 20 22
      src/features/OnlineExam/Examing/ws.js

+ 20 - 22
src/features/OnlineExam/Examing/ws.js

@@ -12,28 +12,10 @@ let reconnectNumber = 0;
 export function openWS({ examRecordDataId }) {
   window._hmt.push(["_trackEvent", "websocket", "准备连接"]);
   console.log("in openWS", examRecordDataId);
-  try {
-    ws = new WebSocket(
-      VUE_APP_WK_SERVER_SOCKET_FOR_AUDIO +
-        `${examRecordDataId}/${store.state.user.key}/${store.state.user.token}`
-    );
-  } catch {
-    setTimeout(() => {
-      // tryWSReconnect();
-      reconnectNumber++;
-      if (reconnectNumber >= 5) {
-        reconnectNumber = 0;
-        Message.error({
-          content: "Websocket重连失败",
-          duration: 5,
-          closable: true
-        });
-      }
-      window._hmt.push(["_trackEvent", "websocket", "初始连接-连接失败"]);
-      openWS({ examRecordDataId });
-    }, RECONNECT_INTERVAL);
-    return;
-  }
+  ws = new WebSocket(
+    VUE_APP_WK_SERVER_SOCKET_FOR_AUDIO +
+      `${examRecordDataId}/${store.state.user.key}/${store.state.user.token}`
+  );
 
   ws.onopen = event => {
     console.log("open ws", event);
@@ -71,6 +53,22 @@ export function openWS({ examRecordDataId }) {
 
     heartbeat();
   };
+
+  ws.onerror = () => {
+    setTimeout(() => {
+      reconnectNumber++;
+      if (reconnectNumber >= 5) {
+        reconnectNumber = 0;
+        Message.error({
+          content: "Websocket重连失败",
+          duration: 5,
+          closable: true
+        });
+      }
+      window._hmt.push(["_trackEvent", "websocket", "连接错误后-重新连接"]);
+      openWS({ examRecordDataId });
+    }, RECONNECT_INTERVAL);
+  };
 }
 
 // function tryWSReconnect() {