Просмотр исходного кода

optimize:优化webSocket链接失败出现的问题

leo лет назад: 2
Родитель
Сommit
82e39ae3ce
3 измененных файлов с 66 добавлено и 40 удалено
  1. 23 19
      static/webRtcYJ/WXdraw.js
  2. 9 0
      static/webRtcYJ/WXtrialInterface.html
  3. 34 21
      static/webRtcYJ/websocket.js

+ 23 - 19
static/webRtcYJ/WXdraw.js

@@ -32,22 +32,23 @@ function changIsControl(value) {
   $('#open-set-phone-size-dialog-btn').attr('hidden', !value);
 }
 
-function throttle(fn, delay) {
-  var flag = true;
-  return () => {
-    if (!flag) return;
-    flag = false;
-    errorTime += delay;
-    timer = setTimeout(() => {
-      fn();
-      flag = true;
-    }, delay);
-  };
-}
+let directivesTimeInterval = 1
+// function throttle(fn, delay) {
+//   var flag = true;
+//   return () => {
+//     if (!flag) return;
+//     flag = false;
+//     errorTime += delay;
+//     timer = setTimeout(() => {
+//       fn();
+//       flag = true;
+//     }, delay);
+//   };
+// }
 
-const throttleDoConnectDirectives = throttle(() => {
-  doConnectDirectives();
-}, 100);
+// const throttleDoConnectDirectives = throttle(() => {
+//   doConnectDirectives();
+// }, 100);
 function doConnectDirectives() {
   videoWidth = Number(resolvingPower) ? Number(resolvingPower) : 720;
   videoHeight = videoWidth === 720 ? 1280 : 1920;
@@ -86,19 +87,22 @@ function doConnectDirectives() {
     );
   };
   wsss.onerror = function (e) {
+    if(!quitBool) return 
+    if (directivesTimeInterval > 7) {
+      quit();
+      return
+    }
     // console.log('🚀 ~ file: WXdraw.js ~ line 82 ~ onerror ~ e', e);
     // wsss.close(1006);
     // throttle(doConnectDirectives, 100);
     // if (errorTime > 1000) {
     //   quit();
     // }
+    directivesTimeInterval++
     // 异常关闭,重连
-    throttleDoConnectDirectives();
+    doConnectDirectives();
     // doConnectDirectives();
     // throttle(doConnectDirectives, 100);
-    if (errorTime > 1000) {
-      quit();
-    }
   };
   wsss.onclose = function (e) {
     // console.log('🚀 ~ file: WXdraw.js ~ line 93 ~ onclose ~ e', e);

+ 9 - 0
static/webRtcYJ/WXtrialInterface.html

@@ -1977,12 +1977,21 @@
           success: function (res) {},
         });
       }
+      // 避免重复退出的字段
+      let quitBool = true
       function quit() {
+          // 退出云机页时,把所有链接都关闭掉
           engine && engine.disconnect();
           wsss&&wsss.close();
           decodeWoker&&decodeWoker.postMessage('close');
           decodeWoker&&decodeWoker.terminate();
           webSocketWorker && webSocketWorker.postMessage('close');
+          if(!quitBool) {
+              return 
+          }
+          quitBool = false
+          // 如果是指令请求失败或者直接退出,都要发送消息给视频流js文件,告知视频流请求失败情况不再做退出操作
+          webSocketWorker && webSocketWorker.postMessage('endRequest');
         if (navigator.userAgent.toLowerCase().includes('toutiaomicroapp')) {
           tt.miniProgram.switchTab({
             url: '/pages/home/home',

+ 34 - 21
static/webRtcYJ/websocket.js

@@ -7,18 +7,20 @@ var socketURL = decodeURIComponent(parameters['socketURL']);
 var intervaler;
 doConnect();
 
-function throttle(fn, delay) {
-  var flag = true;
-  return () => {
-    if (!flag) return;
-    flag = false;
-    errorTime += delay;
-    timer = setTimeout(() => {
-      fn();
-      flag = true;
-    }, delay);
-  };
-}
+// function throttle(fn, delay) {
+//   var flag = true;
+//   return () => {
+//     if (!flag) return;
+//     flag = false;
+//     errorTime += delay;
+//     timer = setTimeout(() => {
+//       fn();
+//       flag = true;
+//     }, delay);
+//   };
+// }
+let timeInterval = 1
+let quitBool = false
 function doConnect() {
   var ws = new WebSocket(socketURL);
   ws.binaryType = 'arraybuffer';
@@ -33,10 +35,15 @@ function doConnect() {
   ws.onerror = function () {
     // ws.close(1006);
     clearInterval(intervaler);
-    throttle(doConnect, 100);
-    if (errorTime > 1000) {
-      self.postMessage('close');
-    }
+    if (quitBool) return
+    if (timeInterval > 7) return self.postMessage('close');
+    timeInterval++
+    doConnect()
+    // if (errorTime > 1000) {
+    //   self.postMessage('close');
+    //   return
+    // }
+    // throttle(doConnect, 100)();
   };
   ws.addEventListener('close', function (e) {
     // if (e.code === 1006) {
@@ -66,11 +73,17 @@ function doConnect() {
   self.addEventListener(
     'message',
     function (e) {
-      if (e.data === 'close') {
-        clearInterval(intervaler);
-        ws.close();
-      } else {
-        ws.send(e.data);
+      switch (e.data) {
+        case 'close':
+          clearInterval(intervaler)
+          ws.close()
+          break;
+        case 'endRequest':
+          quitBool = false
+          break;
+        default:
+          ws.send(e.data);
+          break;
       }
     },
     false,