@ives_xxz/framework 1.0.3 → 1.0.5

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/FW.d.ts CHANGED
@@ -79,30 +79,41 @@ declare namespace FW {
79
79
  node: cc.Node;
80
80
  };
81
81
 
82
- type Sender = {
82
+ type SocketSender = {
83
83
  readonly entry: FW.Entry;
84
84
  initialize?();
85
85
  onDestroy?();
86
+ /** 发送消息 */
86
87
  send(msg: any);
88
+ /** 获取协议Key值 */
89
+ getProtocolKey?(msg: any): string;
90
+ /** 消息发送前回调函数*/
91
+ onBeforeSendingMessage?(msg: any): string;
92
+ /** 是否是心跳消息 */
93
+ onHeart?(msg: any): Promise<boolean>;
87
94
  };
88
- type Handle = {
95
+ type SocketHandle = {
89
96
  readonly entry: FW.Entry;
90
97
  initialize?();
91
98
  onDestroy?();
92
99
  /** 消息处理 */
93
100
  onMessage(msg: any);
94
101
  /** 超时处理 */
95
- onTimeout();
102
+ onTimeout?();
96
103
  /** 错误处理 */
97
- onError(msg: any);
104
+ onError?(msg: any);
98
105
  /** 连接打开 */
99
- onOpen();
106
+ onOpen?();
100
107
  /** 连接关闭 */
101
- onClose();
108
+ onClose?();
102
109
  /** 心跳 */
103
110
  onHeart?(msg: any): Promise<boolean>;
104
111
  /** 弱网 */
105
112
  onWeakNetWork?(): void;
113
+ /** 获取协议Key值 */
114
+ getProtocolKey?(msg: any): string;
115
+ /** 消息收到前回调函数 */
116
+ onBeforeReceivingMessage?(msg: any): Promise<object>;
106
117
  };
107
118
 
108
119
  type ObjectManager = {
@@ -157,19 +168,14 @@ declare namespace FW {
157
168
  put(uniqueId: number);
158
169
  onDestroy();
159
170
  };
171
+
160
172
  type SocketManager = {
161
173
  createSocket(
162
174
  tag: string,
163
175
  address: string,
164
176
  sender: SocketSender,
165
177
  handle: SocketHandle,
166
- config: {
167
- heartTimeout?: number;
168
- heartWeakTime?: number;
169
- heartInternal?: number;
170
- maxReconnectTimes?: number;
171
- reconnectInternal?: number;
172
- },
178
+ config: SocketConfig,
173
179
  ): Promise<Socket>;
174
180
  /**
175
181
  * 获取socket
@@ -251,17 +257,21 @@ declare namespace FW {
251
257
  createSocket(
252
258
  address: string,
253
259
  tag: string,
254
- sender: FWSocketSender,
255
- handle: FWSocketHandle,
256
- config: {
257
- heartInternal: number;
258
- heartTimeout: number;
259
- heartWeakTime: number;
260
- maxReconnectTimes: number;
261
- },
260
+ sender: SocketSender,
261
+ handle: SocketHandle,
262
+ config: SocketConfig,
262
263
  ): SocketPromiseProxy;
263
264
  };
264
265
 
266
+ type SocketConfig = {
267
+ heartInternal: number;
268
+ heartTimeout: number;
269
+ heartWeakTime: number;
270
+ maxReconnectTimes: number;
271
+ reconnectInternal: number;
272
+ protocolSymbol: Symbol;
273
+ };
274
+
265
275
  /**
266
276
  * 观察者
267
277
  */
@@ -303,6 +313,8 @@ declare namespace FW {
303
313
 
304
314
  type TargetType = cc.Component | Record<string, any> | cc.Node;
305
315
 
316
+ type Symbol = string | number;
317
+
306
318
  type UiManager = {
307
319
  registerButtonEvent(args: FW.RegisterEventArgs | FW.RegisterEventArgs[]);
308
320
  pauseEvent<T extends TargetType>(target: T);
@@ -1378,11 +1390,11 @@ declare namespace FW {
1378
1390
  /**
1379
1391
  * socket发送者
1380
1392
  */
1381
- sender?: Newable<Sender>;
1393
+ sender?: Newable<SocketSender>;
1382
1394
  /**
1383
1395
  * socket处理者
1384
1396
  */
1385
- handle?: Newable<Handle>;
1397
+ handle?: Newable<SocketHandle>;
1386
1398
  };
1387
1399
 
1388
1400
  /**
@@ -0,0 +1,10 @@
1
+ export namespace FWSystemConfig {
2
+ export const SocketDefaultConfig = {
3
+ heartInternal: 3000,
4
+ heartTimeout: 30000,
5
+ heartWeakTime: 1000,
6
+ maxReconnectTimes: 10,
7
+ reconnectInternal: 3000,
8
+ protocolSymbol: null,
9
+ };
10
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "ver": "1.1.0",
3
+ "uuid": "de945287-2149-4870-b846-761e79868e93",
4
+ "importer": "typescript",
5
+ "isPlugin": false,
6
+ "loadPluginInWeb": true,
7
+ "loadPluginInNative": true,
8
+ "loadPluginInEditor": false,
9
+ "subMetas": {}
10
+ }
@@ -25,22 +25,17 @@ export default class FWSocketManager extends FWManager implements FW.SocketManag
25
25
  async createSocket(
26
26
  tag: string,
27
27
  address: string,
28
- sender: FWSocketSender,
29
- handle: FWSocketHandle,
30
- config: {
31
- heartTimeout: number;
32
- heartWeakTime: number;
33
- heartInternal: number;
34
- maxReconnectTimes: number;
35
- reconnectInternal: number;
36
- },
28
+ sender: FW.SocketSender,
29
+ handle: FW.SocketHandle,
30
+ config: FW.SocketConfig,
37
31
  ) {
38
32
  const proxy = new FWSocket().createSocket(address, tag, sender, handle, {
39
- heartTimeout: config?.heartTimeout,
40
- heartWeakTime: config?.heartWeakTime,
41
- heartInternal: config?.heartInternal,
42
- maxReconnectTimes: config?.maxReconnectTimes,
43
- reconnectInternal: config?.reconnectInternal,
33
+ heartTimeout: config.heartTimeout,
34
+ heartWeakTime: config.heartWeakTime,
35
+ heartInternal: config.heartInternal,
36
+ maxReconnectTimes: config.maxReconnectTimes,
37
+ reconnectInternal: config.reconnectInternal,
38
+ protocolSymbol: config.protocolSymbol,
44
39
  });
45
40
  this.socketMap.set(tag, proxy.socket);
46
41
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ives_xxz/framework",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "cocoscreator 2.x mvc framework",
5
5
  "main": "index.js",
6
6
  "keywords": [
@@ -1,7 +1,7 @@
1
- import Framework from '../../Framework';
2
- import FWLog from '../../log/FWLog';
3
- import FWSocketHandle from './FWSocketHandle';
4
- import FWSocketSender from './FWSocketSender';
1
+ import { FWSystemConfig } from "../../config/FWSystemConfig";
2
+ import FWLog from "../../log/FWLog";
3
+ import FWSocketHandle from "./FWSocketHandle";
4
+ import FWSocketSender from "./FWSocketSender";
5
5
 
6
6
  /**
7
7
  * TODO 犹豫socket没有改版暂时已老的cmd字符串映射方式做,后期优化
@@ -37,8 +37,15 @@ export default class FWSocket implements FW.Socket {
37
37
  protected isPausedMessageHandle: boolean = false;
38
38
  /** 是否正在重连 */
39
39
  private isReconnect: boolean = false;
40
-
40
+ /** 代理 */
41
41
  private promiseProxy: FW.SocketPromiseProxy;
42
+ /** 协议符号 */
43
+ private protocolSymbol: FW.Symbol;
44
+ /** 协议注册表 */
45
+ private protocolRegistry: Map<string, Set<Symbol>>;
46
+ /** 协议符号容器 */
47
+ private protocolContainer: Map<Symbol, string>;
48
+
42
49
  /**
43
50
  * 消息事件列表
44
51
  * TODO 目前没有消息协议号,暂时无用
@@ -51,24 +58,29 @@ export default class FWSocket implements FW.Socket {
51
58
 
52
59
  public initialize(): void {
53
60
  this.messageEvents = cc.js.createMap();
61
+ this.protocolContainer = new Map<Symbol, string>();
62
+ this.protocolRegistry = new Map<string, Set<Symbol>>();
54
63
  }
55
64
 
56
65
  createSocket(
57
66
  address: string,
58
67
  tag: string,
59
- sender: FWSocketSender,
60
- handle: FWSocketHandle,
61
- config: {
62
- heartInternal: number;
63
- heartTimeout: number;
64
- heartWeakTime: number;
65
- maxReconnectTimes: number;
66
- reconnectInternal: number;
67
- },
68
+ sender: FW.SocketSender,
69
+ handle: FW.SocketHandle,
70
+ config: FW.SocketConfig
68
71
  ): FW.SocketPromiseProxy {
69
72
  FW.Entry.timeMgr.unSchedule(this);
73
+
70
74
  this.tag = tag;
71
75
  this.address = address;
76
+
77
+ this.messageEvents = {};
78
+ this.protocolContainer.clear();
79
+ this.protocolRegistry.clear();
80
+ this.isReconnect = true;
81
+ this.sendHeartTimestamp = 0;
82
+ this.receiveTimeStamp = new Date().getTime();
83
+
72
84
  this.socket = new WebSocket(address);
73
85
  this.socket.onopen = this.onSocketOpen.bind(this);
74
86
  this.socket.onclose = this.onSocketClose.bind(this);
@@ -78,15 +90,22 @@ export default class FWSocket implements FW.Socket {
78
90
  this.sender = sender;
79
91
  this.handle = handle;
80
92
 
81
- this.heartInternal = config?.heartInternal;
82
- this.heartTimeout = config?.heartTimeout;
83
- this.heartWeakTime = config?.heartWeakTime;
84
- this.maxReconnectTimes = config?.maxReconnectTimes;
93
+ const defaultConfig = FWSystemConfig.SocketDefaultConfig;
94
+ this.heartInternal = config?.heartInternal || defaultConfig.heartInternal;
95
+ this.heartTimeout = config?.heartTimeout || defaultConfig.heartTimeout;
96
+ this.heartWeakTime = config?.heartWeakTime || defaultConfig.heartWeakTime;
97
+ this.maxReconnectTimes =
98
+ config?.maxReconnectTimes || defaultConfig.maxReconnectTimes;
99
+ this.reconnectInternal =
100
+ config?.reconnectInternal || defaultConfig.reconnectInternal;
101
+ this.protocolSymbol =
102
+ config?.protocolSymbol || defaultConfig.protocolSymbol;
85
103
 
86
104
  this.promiseProxy = {
87
105
  promise: undefined,
88
106
  socket: this,
89
107
  };
108
+
90
109
  new Promise<FWSocket>((resolve) => {
91
110
  this.promiseProxy.promise = resolve;
92
111
  });
@@ -146,16 +165,13 @@ export default class FWSocket implements FW.Socket {
146
165
  }
147
166
 
148
167
  FWLog.debug(
149
- `socket reconnect : reconnectTimes->${this.reconnectTimes},maxReconnectTimes->${this.maxReconnectTimes}`,
168
+ `socket reconnect : reconnectTimes->${this.reconnectTimes},maxReconnectTimes->${this.maxReconnectTimes}`
150
169
  );
151
170
  if (++this.reconnectTimes >= this.maxReconnectTimes) {
152
171
  this.handle?.onTimeout?.();
153
172
  return;
154
173
  }
155
- FW.Entry.timeMgr.unSchedule(this);
156
- this.isReconnect = true;
157
- this.sendHeartTimestamp = 0;
158
- this.receiveTimeStamp = 0;
174
+
159
175
  this.socket.close();
160
176
  this.socket = null;
161
177
 
@@ -165,16 +181,33 @@ export default class FWSocket implements FW.Socket {
165
181
  heartWeakTime: this.heartWeakTime,
166
182
  maxReconnectTimes: this.maxReconnectTimes,
167
183
  reconnectInternal: this.reconnectInternal,
184
+ protocolSymbol: this.protocolSymbol,
168
185
  });
169
186
  }
170
187
 
188
+ async send(msg: any) {
189
+ if (this.getReadyState() != WebSocket.OPEN) return;
190
+ if (this.isPausedMessageHandle) return;
191
+ if (!(await this.sender.onHeart?.(msg))) {
192
+ const protocolKey = this.sender.getProtocolKey?.(msg);
193
+ if (protocolKey) {
194
+ const symbol = Symbol(protocolKey);
195
+ const registry =
196
+ this.protocolRegistry.get(protocolKey) || new Set<Symbol>();
197
+ this.protocolRegistry.set(protocolKey, registry.add(symbol));
198
+ this.protocolContainer.set(symbol, msg);
199
+ }
200
+ }
201
+ this.sendMessage(msg);
202
+ }
203
+
171
204
  /** 连接打开 */
172
205
  private onSocketOpen() {
173
- FWLog.debug('on open!');
206
+ FWLog.debug("on open!");
174
207
  FW.Entry.timeMgr.unSchedule(this);
175
208
  this.reconnectTimes = 0;
176
209
  this.sendHeartTimestamp = 0;
177
- this.receiveTimeStamp = 0;
210
+ this.receiveTimeStamp = new Date().getTime();
178
211
  this.isReconnect = false;
179
212
  this.handle?.onOpen?.();
180
213
  this.heartHandle();
@@ -193,10 +226,16 @@ export default class FWSocket implements FW.Socket {
193
226
  }
194
227
  }
195
228
  this.sendHeart();
229
+
230
+ if (this.protocolContainer.size > 0) {
231
+ this.protocolContainer?.forEach((msg) => {
232
+ this.sendMessage(msg);
233
+ });
234
+ }
196
235
  },
197
236
  this.heartInternal / 1000,
198
237
  cc.macro.REPEAT_FOREVER,
199
- this,
238
+ this
200
239
  );
201
240
  }
202
241
 
@@ -205,10 +244,12 @@ export default class FWSocket implements FW.Socket {
205
244
  this.sender?.sendHeart?.();
206
245
  }
207
246
 
208
- send(msg: any) {
209
- if (this.getReadyState() != WebSocket.OPEN) return;
210
- if (this.isPausedMessageHandle) return;
211
- this.socket?.send(msg);
247
+ private sendMessage(msg: any) {
248
+ try {
249
+ this.socket?.send(this.sender.onBeforeSendingMessage?.(msg) || msg);
250
+ } catch (e) {
251
+ this.socket?.send(msg);
252
+ }
212
253
  }
213
254
 
214
255
  /** socket关闭 */
@@ -217,24 +258,50 @@ export default class FWSocket implements FW.Socket {
217
258
  FW.Entry.timeMgr.scheduleOnce(
218
259
  () => {
219
260
  if (!this.isReconnect) return;
220
- FWLog.debug('on close!');
261
+ FWLog.debug("on close!");
221
262
  this.reconnect();
222
263
  },
223
- 3,
224
- this,
264
+ this.reconnectInternal,
265
+ this
225
266
  );
226
267
  }
227
268
  /** 消息处理 */
228
- private async onSocketMessage(msg: any) {
269
+ private async onSocketMessage(originalMsg: any) {
270
+ const msg = await this.handle.onBeforeReceivingMessage(originalMsg);
271
+
229
272
  if (await this.handle?.onHeart?.(msg)) {
230
273
  this.receiveTimeStamp = new Date().getTime();
231
- } else {
232
- if (this.isPausedMessageHandle) return;
233
- this.handle?.onMessage?.(msg);
274
+ return;
234
275
  }
276
+
277
+ this.remoteProtocol(msg);
278
+
279
+ if (this.isPausedMessageHandle) return;
280
+
281
+ this.handle?.onMessage?.(msg);
235
282
  }
236
283
  /** 错误处理 */
237
- private onSocketError(msg: any) {
284
+ private async onSocketError(msg: any) {
285
+ await this.remoteProtocol(msg);
238
286
  this.handle?.onError?.(msg);
239
287
  }
288
+
289
+ private async remoteProtocol(msg: any) {
290
+ const protocolKey = await this.handle.getProtocolKey?.(msg);
291
+ if (protocolKey && this.protocolRegistry.has(protocolKey)) {
292
+ const symbolSet = this.protocolRegistry.get(protocolKey);
293
+
294
+ if (symbolSet && symbolSet.size > 0) {
295
+ const [symbol] = symbolSet;
296
+
297
+ symbolSet.delete(symbol);
298
+
299
+ this.protocolContainer.delete(symbol);
300
+
301
+ if (symbolSet.size === 0) {
302
+ this.protocolRegistry.delete(protocolKey);
303
+ }
304
+ }
305
+ }
306
+ }
240
307
  }
@@ -1,17 +1,19 @@
1
1
  import { injectable } from 'inversify';
2
2
  @injectable()
3
- export default abstract class FWSocketHandle implements FW.Handle {
3
+ export default abstract class FWSocketHandle implements FW.SocketHandle {
4
4
  constructor() {
5
5
  this.initialize?.();
6
6
  }
7
7
  public readonly entry: FW.Entry = FW.Entry;
8
+ abstract onMessage(msg: any): void;
8
9
  initialize?();
9
10
  onDestroy?();
10
11
  onHeart?(msg: any): Promise<boolean>;
11
12
  onWeakNetWork?(): void;
12
- abstract onClose(): void;
13
- abstract onOpen(): void;
14
- abstract onMessage(msg: any): void;
15
- abstract onError(msg: any): void;
16
- abstract onTimeout(): void;
13
+ onClose?(): void;
14
+ onOpen?(): void;
15
+ onError?(msg: any): void;
16
+ onTimeout?(): void;
17
+ getProtocolKey?(msg: any): string;
18
+ onBeforeReceivingMessage?(msg: any): Promise<object>;
17
19
  }
@@ -1,7 +1,7 @@
1
1
  import { injectable } from 'inversify';
2
2
  import FWLog from '../../log/FWLog';
3
3
  @injectable()
4
- export default class FWSocketSender implements FW.Sender {
4
+ export default class FWSocketSender implements FW.SocketSender {
5
5
  constructor() {
6
6
  this.initialize?.();
7
7
  }
@@ -13,4 +13,7 @@ export default class FWSocketSender implements FW.Sender {
13
13
  send(msg: any) {
14
14
  FW.Entry.socketMgr.getSocket()?.send(msg);
15
15
  }
16
+ onHeart?(msg: any): Promise<boolean>;
17
+ onBeforeSendingMessage?(msg: any): string;
18
+ getProtocolKey?(msg: any): string;
16
19
  }