@ives_xxz/framework 1.2.7 → 1.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ives_xxz/framework",
3
- "version": "1.2.7",
3
+ "version": "1.2.8",
4
4
  "description": "cocoscreator 2.x mvc framework",
5
5
  "main": "index.js",
6
6
  "keywords": [
@@ -35,8 +35,6 @@ export default class FWSocket implements FW.Socket {
35
35
  protected tag: string;
36
36
  /** 是否暂停消息处理 */
37
37
  protected isPausedMessageHandle: boolean = false;
38
- /** 是否正在重连 */
39
- private isReconnect: boolean = false;
40
38
  /** 代理 */
41
39
  private promiseProxy: FW.SocketPromiseProxy;
42
40
  /** 协议符号 */
@@ -70,46 +68,49 @@ export default class FWSocket implements FW.Socket {
70
68
  handle: FW.SocketHandle,
71
69
  config: FW.SocketConfig,
72
70
  ): FW.SocketPromiseProxy {
73
- FW.Entry.timeMgr.unSchedule(this);
71
+ try {
72
+ FW.Entry.timeMgr.unSchedule(this);
74
73
 
75
- this.tag = tag;
76
- this.address = address;
74
+ this.tag = tag;
75
+ this.address = address;
77
76
 
78
- this.messageEvents = {};
79
- this.protocolContainer.clear();
80
- this.protocolRegistry.clear();
81
- this.isReconnect = true;
82
- this.sendHeartTimestamp = 0;
83
- this.receiveTimeStamp = new Date().getTime();
84
- FWLog.debug('创建socket -> ', address);
85
- this.socket = new WebSocket(address);
86
- this.socket.onopen = this.onSocketOpen.bind(this);
87
- this.socket.onclose = this.onSocketClose.bind(this);
88
- this.socket.onerror = this.onSocketError.bind(this);
89
- this.socket.onmessage = this.onSocketMessage.bind(this);
90
-
91
- this.sender = sender;
92
- this.handle = handle;
93
-
94
- const defaultConfig = FWSystemConfig.SocketDefaultConfig;
95
- this.heartInternal = config?.heartInternal || defaultConfig.heartInternal;
96
- this.heartTimeout = config?.heartTimeout || defaultConfig.heartTimeout;
97
- this.heartWeakTime = config?.heartWeakTime || defaultConfig.heartWeakTime;
98
- this.maxReconnectTimes = config?.maxReconnectTimes || defaultConfig.maxReconnectTimes;
99
- this.reconnectInternal = config?.reconnectInternal || defaultConfig.reconnectInternal;
100
- this.protocolSymbol = config?.protocolSymbol || defaultConfig.protocolSymbol;
101
-
102
- this.promiseProxy = {
103
- promise: undefined,
104
- resolve: undefined,
105
- socket: this,
106
- };
107
-
108
- this.promiseProxy.promise = new Promise<FW.Socket>((resolve) => {
109
- this.promiseProxy.resolve = resolve;
110
- });
77
+ this.messageEvents = {};
78
+ this.protocolContainer.clear();
79
+ this.protocolRegistry.clear();
80
+ this.sendHeartTimestamp = 0;
81
+ this.receiveTimeStamp = new Date().getTime();
111
82
 
112
- return this.promiseProxy;
83
+ this.socket = new WebSocket(address);
84
+ this.socket.onopen = this.onSocketOpen.bind(this);
85
+ this.socket.onclose = this.onSocketClose.bind(this);
86
+ this.socket.onerror = this.onSocketError.bind(this);
87
+ this.socket.onmessage = this.onSocketMessage.bind(this);
88
+
89
+ this.sender = sender;
90
+ this.handle = handle;
91
+
92
+ const defaultConfig = FWSystemConfig.SocketDefaultConfig;
93
+ this.heartInternal = config?.heartInternal || defaultConfig.heartInternal;
94
+ this.heartTimeout = config?.heartTimeout || defaultConfig.heartTimeout;
95
+ this.heartWeakTime = config?.heartWeakTime || defaultConfig.heartWeakTime;
96
+ this.maxReconnectTimes = config?.maxReconnectTimes || defaultConfig.maxReconnectTimes;
97
+ this.reconnectInternal = config?.reconnectInternal || defaultConfig.reconnectInternal;
98
+ this.protocolSymbol = config?.protocolSymbol || defaultConfig.protocolSymbol;
99
+
100
+ this.promiseProxy = {
101
+ promise: undefined,
102
+ resolve: undefined,
103
+ socket: this,
104
+ };
105
+
106
+ this.promiseProxy.promise = new Promise<FW.Socket>((resolve) => {
107
+ this.promiseProxy.resolve = resolve;
108
+ });
109
+
110
+ return this.promiseProxy;
111
+ } catch (e) {
112
+ FWLog.error('创建socket失败:', e);
113
+ }
113
114
  }
114
115
  /**
115
116
  * 注册消息事件
@@ -142,7 +143,7 @@ export default class FWSocket implements FW.Socket {
142
143
  * @returns
143
144
  */
144
145
  public getReadyState() {
145
- return this.socket.readyState;
146
+ return this.socket?.readyState;
146
147
  }
147
148
  /**
148
149
  * 关闭连接
@@ -159,6 +160,10 @@ export default class FWSocket implements FW.Socket {
159
160
  this.isPausedMessageHandle = false;
160
161
  }
161
162
 
163
+ public get isReconnect() {
164
+ return this.getReadyState() === WebSocket.CONNECTING;
165
+ }
166
+
162
167
  reconnect(force?: boolean) {
163
168
  if (force) this.reconnectTimes = 0;
164
169
 
@@ -205,7 +210,6 @@ export default class FWSocket implements FW.Socket {
205
210
  this.reconnectTimes = 0;
206
211
  this.sendHeartTimestamp = 0;
207
212
  this.receiveTimeStamp = new Date().getTime();
208
- this.isReconnect = false;
209
213
  this.handle?.onOpen?.();
210
214
  this.heartHandle();
211
215
  this.promiseProxy?.resolve(this);
@@ -256,9 +260,10 @@ export default class FWSocket implements FW.Socket {
256
260
  this.handle?.onClose?.();
257
261
  FW.Entry.timeMgr.scheduleOnce(
258
262
  () => {
259
- if (!this.isReconnect) return;
260
- FWLog.debug('on close!');
261
- this.reconnect();
263
+ if (this.getReadyState() == WebSocket.CLOSING || this.getReadyState() == WebSocket.CLOSED) {
264
+ FWLog.debug('on close!');
265
+ this.reconnect();
266
+ }
262
267
  },
263
268
  this.reconnectInternal,
264
269
  this,