@ives_xxz/framework 1.0.3 → 1.0.4
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 +35 -23
- package/config/FWSystemConfig.ts +10 -0
- package/config/FWSystemConfig.ts.meta +10 -0
- package/manager/FWSocketManager.ts +9 -14
- package/package.json +1 -1
- package/service/socket/FWSocket.ts +82 -29
- package/service/socket/FWSocketHandle.ts +8 -6
- package/service/socket/FWSocketSender.ts +4 -1
package/FW.d.ts
CHANGED
|
@@ -79,30 +79,41 @@ declare namespace FW {
|
|
|
79
79
|
node: cc.Node;
|
|
80
80
|
};
|
|
81
81
|
|
|
82
|
-
type
|
|
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
|
|
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:
|
|
255
|
-
handle:
|
|
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<
|
|
1393
|
+
sender?: Newable<SocketSender>;
|
|
1382
1394
|
/**
|
|
1383
1395
|
* socket处理者
|
|
1384
1396
|
*/
|
|
1385
|
-
handle?: Newable<
|
|
1397
|
+
handle?: Newable<SocketHandle>;
|
|
1386
1398
|
};
|
|
1387
1399
|
|
|
1388
1400
|
/**
|
|
@@ -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:
|
|
29
|
-
handle:
|
|
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
|
|
40
|
-
heartWeakTime: config
|
|
41
|
-
heartInternal: config
|
|
42
|
-
maxReconnectTimes: config
|
|
43
|
-
reconnectInternal: config
|
|
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,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { FWSystemConfig } from '../../config/FWSystemConfig';
|
|
2
2
|
import FWLog from '../../log/FWLog';
|
|
3
3
|
import FWSocketHandle from './FWSocketHandle';
|
|
4
4
|
import FWSocketSender from './FWSocketSender';
|
|
@@ -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:
|
|
60
|
-
handle:
|
|
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,19 @@ export default class FWSocket implements FW.Socket {
|
|
|
78
90
|
this.sender = sender;
|
|
79
91
|
this.handle = handle;
|
|
80
92
|
|
|
81
|
-
|
|
82
|
-
this.
|
|
83
|
-
this.
|
|
84
|
-
this.
|
|
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 = config?.maxReconnectTimes || defaultConfig.maxReconnectTimes;
|
|
98
|
+
this.reconnectInternal = config?.reconnectInternal || defaultConfig.reconnectInternal;
|
|
99
|
+
this.protocolSymbol = config?.protocolSymbol || defaultConfig.protocolSymbol;
|
|
85
100
|
|
|
86
101
|
this.promiseProxy = {
|
|
87
102
|
promise: undefined,
|
|
88
103
|
socket: this,
|
|
89
104
|
};
|
|
105
|
+
|
|
90
106
|
new Promise<FWSocket>((resolve) => {
|
|
91
107
|
this.promiseProxy.promise = resolve;
|
|
92
108
|
});
|
|
@@ -152,10 +168,7 @@ export default class FWSocket implements FW.Socket {
|
|
|
152
168
|
this.handle?.onTimeout?.();
|
|
153
169
|
return;
|
|
154
170
|
}
|
|
155
|
-
|
|
156
|
-
this.isReconnect = true;
|
|
157
|
-
this.sendHeartTimestamp = 0;
|
|
158
|
-
this.receiveTimeStamp = 0;
|
|
171
|
+
|
|
159
172
|
this.socket.close();
|
|
160
173
|
this.socket = null;
|
|
161
174
|
|
|
@@ -165,16 +178,32 @@ export default class FWSocket implements FW.Socket {
|
|
|
165
178
|
heartWeakTime: this.heartWeakTime,
|
|
166
179
|
maxReconnectTimes: this.maxReconnectTimes,
|
|
167
180
|
reconnectInternal: this.reconnectInternal,
|
|
181
|
+
protocolSymbol: this.protocolSymbol,
|
|
168
182
|
});
|
|
169
183
|
}
|
|
170
184
|
|
|
185
|
+
async send(msg: any) {
|
|
186
|
+
if (this.getReadyState() != WebSocket.OPEN) return;
|
|
187
|
+
if (this.isPausedMessageHandle) return;
|
|
188
|
+
if (!(await this.sender.onHeart?.(msg))) {
|
|
189
|
+
const protocolKey = this.sender.getProtocolKey?.(msg);
|
|
190
|
+
if (protocolKey) {
|
|
191
|
+
const symbol = Symbol(protocolKey);
|
|
192
|
+
const registry = this.protocolRegistry.get(protocolKey) || new Set<Symbol>();
|
|
193
|
+
this.protocolRegistry.set(protocolKey, registry.add(symbol));
|
|
194
|
+
this.protocolContainer.set(symbol, msg);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
this.sendMessage(msg);
|
|
198
|
+
}
|
|
199
|
+
|
|
171
200
|
/** 连接打开 */
|
|
172
201
|
private onSocketOpen() {
|
|
173
202
|
FWLog.debug('on open!');
|
|
174
203
|
FW.Entry.timeMgr.unSchedule(this);
|
|
175
204
|
this.reconnectTimes = 0;
|
|
176
205
|
this.sendHeartTimestamp = 0;
|
|
177
|
-
this.receiveTimeStamp =
|
|
206
|
+
this.receiveTimeStamp = new Date().getTime();
|
|
178
207
|
this.isReconnect = false;
|
|
179
208
|
this.handle?.onOpen?.();
|
|
180
209
|
this.heartHandle();
|
|
@@ -193,6 +222,12 @@ export default class FWSocket implements FW.Socket {
|
|
|
193
222
|
}
|
|
194
223
|
}
|
|
195
224
|
this.sendHeart();
|
|
225
|
+
|
|
226
|
+
if (this.protocolContainer.size > 0) {
|
|
227
|
+
this.protocolContainer?.forEach((msg) => {
|
|
228
|
+
this.sendMessage(msg);
|
|
229
|
+
});
|
|
230
|
+
}
|
|
196
231
|
},
|
|
197
232
|
this.heartInternal / 1000,
|
|
198
233
|
cc.macro.REPEAT_FOREVER,
|
|
@@ -205,10 +240,8 @@ export default class FWSocket implements FW.Socket {
|
|
|
205
240
|
this.sender?.sendHeart?.();
|
|
206
241
|
}
|
|
207
242
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
if (this.isPausedMessageHandle) return;
|
|
211
|
-
this.socket?.send(msg);
|
|
243
|
+
private sendMessage(msg: any) {
|
|
244
|
+
this.socket?.send(this.sender.onBeforeSendingMessage?.(msg) || msg);
|
|
212
245
|
}
|
|
213
246
|
|
|
214
247
|
/** socket关闭 */
|
|
@@ -220,18 +253,38 @@ export default class FWSocket implements FW.Socket {
|
|
|
220
253
|
FWLog.debug('on close!');
|
|
221
254
|
this.reconnect();
|
|
222
255
|
},
|
|
223
|
-
|
|
256
|
+
this.reconnectInternal,
|
|
224
257
|
this,
|
|
225
258
|
);
|
|
226
259
|
}
|
|
227
260
|
/** 消息处理 */
|
|
228
|
-
private async onSocketMessage(
|
|
261
|
+
private async onSocketMessage(originalMsg: any) {
|
|
262
|
+
const msg = await this.handle.onBeforeReceivingMessage(originalMsg);
|
|
263
|
+
|
|
229
264
|
if (await this.handle?.onHeart?.(msg)) {
|
|
230
265
|
this.receiveTimeStamp = new Date().getTime();
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const protocolKey = await this.handle.getProtocolKey?.(msg);
|
|
270
|
+
if (protocolKey && this.protocolRegistry.has(protocolKey)) {
|
|
271
|
+
const symbolSet = this.protocolRegistry.get(protocolKey);
|
|
272
|
+
|
|
273
|
+
if (symbolSet && symbolSet.size > 0) {
|
|
274
|
+
const [symbol] = symbolSet;
|
|
275
|
+
|
|
276
|
+
symbolSet.delete(symbol);
|
|
277
|
+
|
|
278
|
+
this.protocolContainer.delete(symbol);
|
|
279
|
+
|
|
280
|
+
if (symbolSet.size === 0) {
|
|
281
|
+
this.protocolRegistry.delete(protocolKey);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
234
284
|
}
|
|
285
|
+
|
|
286
|
+
if (this.isPausedMessageHandle) return;
|
|
287
|
+
this.handle?.onMessage?.(msg);
|
|
235
288
|
}
|
|
236
289
|
/** 错误处理 */
|
|
237
290
|
private onSocketError(msg: any) {
|
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import { injectable } from 'inversify';
|
|
2
2
|
@injectable()
|
|
3
|
-
export default abstract class FWSocketHandle implements FW.
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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.
|
|
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
|
}
|