Parcourir la source

优化WebSocket断线后重连的等待时间

li.shaoyi il y a 3 ans
Parent
commit
ea6fe4ca90
1 fichiers modifiés avec 10 ajouts et 12 suppressions
  1. 10 12
      src/utils/websocket/index.ts

+ 10 - 12
src/utils/websocket/index.ts

@@ -83,7 +83,7 @@ export class MTP2WebSocket<T extends Package40 | Package50> {
     private socket?: WebSocket;
     /** 当前流水号 */
     private currentSerial = 1;
-    /** 默认超时时长(秒) */
+    /** 默认请求超时时长(秒) */
     private timeOutInterval = 30 * 1000;
     /** 信息发送异步建值对 */
     private asyncTaskMap: Map<string, AsyncTask> = new Map();
@@ -102,8 +102,8 @@ export class MTP2WebSocket<T extends Package40 | Package50> {
     private timeoutTimer = 0;
     /** 心跳间隔时间,默认为10秒 */
     private beatInterval = 10 * 1000;
-    /**  心跳回复超时时间,默认为30秒 */
-    private beatTimeoutInterval = 30 * 1000;
+    /**  心跳回复超时时间,默认为15秒 */
+    private beatTimeoutInterval = 15 * 1000;
     /** 外部是否要求停止断网重连操作标志 */
     private isBrokenReconnecting = false;
 
@@ -145,15 +145,20 @@ export class MTP2WebSocket<T extends Package40 | Package50> {
                 this.socket.onclose = () => {
                     console.warn(this.packageType, this.host, '连接已断开');
                     this.socket = undefined;
+                    this.readyState = undefined;
                     this.connState = 'Unconnected';
                     this.onClosed && this.onClosed(this);
+
+                    // 如果重连过程中主动断开了就不再进行重连
+                    if (!this.isBrokenReconnecting) {
+                        this.reconnect();
+                    }
                 }
                 // 连接发生错误
                 this.socket.onerror = () => {
                     const message = this.host + '连接发生错误';
                     this.onError && this.onError(this, new Error(message));
                     this.callAllAsyncTaskOnReconnecting();  // 回调当前所有发送信息错误块
-                    this.reconnect();
                     reject(message);
                 }
                 // 接收数据
@@ -174,8 +179,6 @@ export class MTP2WebSocket<T extends Package40 | Package50> {
     close() {
         clearTimeout(this.reconnectTimer);
         this.stopBeatTimer();
-        this.readyState = undefined;
-        this.connState = 'Unconnected';
         this.isBrokenReconnecting = true;
         this.reconnectCount = 0;
         this.socket?.close();
@@ -461,14 +464,9 @@ export class MTP2WebSocket<T extends Package40 | Package50> {
                 this.reconnectCount = 0;
                 this.onReconnectChangeState && this.onReconnectChangeState(this, ReconnectChangeState.ReconnectSuccessed);
             }).catch(() => {
-                this.readyState = undefined;
-                this.onReconnectChangeState && this.onReconnectChangeState(this, ReconnectChangeState.FailAndWaitPeriod);
-
-                // 重连失败处理,如果重连过程中主动断开的了就不再进行重连
                 if (!this.isBrokenReconnecting) {
                     console.log(this.packageType, this.host, `第${this.reconnectCount}次重连失败`);
-                    this.connState = 'Unconnected';
-                    this.reconnect();
+                    this.onReconnectChangeState && this.onReconnectChangeState(this, ReconnectChangeState.FailAndWaitPeriod);
                 }
             })
         }, this.reconnectInterval);