Преглед на файлове

优化: 关闭页面时关闭连接及实例,释放内存

t_finder преди 3 дни
родител
ревизия
d603d52243
променени са 1 файла, в които са добавени 18 реда и са изтрити 12 реда
  1. 18 12
      pages/rtcEngine/rtc.vue

+ 18 - 12
pages/rtcEngine/rtc.vue

@@ -254,6 +254,8 @@ export default {
   },
   // 页面销毁前触发
   beforeDestroy() {
+    // 关闭通道
+    this.closeChannel();
     // 销毁页面监听事件
     this.destroyListener();
   },
@@ -762,12 +764,7 @@ export default {
         this.activeCloud = cloudData;
 
         // 重置相关数据
-        // 关闭日志上报
-        this.logReportObj?.destroy();
-        // 关闭webRTC
-        this.engine?.disconnect && this.engine?.disconnect();
-        // 关闭业务指令通道
-        this.doConnectDirectivesWs?.close();
+        this.closeChannel();
         // 重置重连次数
         this.doConnectDirectivesRequestNum = 1;
         // 重置 end
@@ -813,12 +810,8 @@ export default {
     },
     // 退出功能
     exit() {
-      // 关闭日志上报
-      this.logReportObj?.destroy();
-      // 关闭webRTC
-      this.engine?.disconnect && this.engine?.disconnect();
-      // 关闭业务指令通道
-      this.doConnectDirectivesWs?.close();
+      // 关闭通道
+      this.closeChannel();
 
       // 获取当前环境
       const env = isBrowserEnvironment();
@@ -851,6 +844,19 @@ export default {
         this.logReportObj.setParams({ clientType: Object.keys(res)[0] });
       })
     },
+    // 关闭通道
+    async closeChannel() {
+      try {
+        // 关闭日志上报
+        this.logReportObj?.destroy();
+        // 关闭webRTC
+        this.engine?.disconnect && this.engine?.disconnect();
+        // 关闭业务指令通道
+        this.doConnectDirectivesWs?.close();
+      } catch (error) {
+        console.log('closeChannel error', error);
+      }
+    },
     // 开始运行程序
     async start(activeCloud) {
       console.log('start activeCloud', activeCloud);