@ives_xxz/framework 1.5.5 → 1.5.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/FW.d.ts +129 -56
- package/Framework.ts +64 -24
- package/FrameworkBase.ts +58 -26
- package/controller/FWLayerController.ts +17 -12
- package/data/FWData.ts +3 -5
- package/logic/FWLogic.ts +4 -6
- package/manager/FWLayerManager.ts +124 -69
- package/manager/FWPromiseManager.ts +69 -38
- package/manager/FWTaskManager.ts +2 -2
- package/package.json +4 -2
- package/service/FWService.ts +2 -4
- package/service/socket/FWSocket.ts +44 -33
- package/service/socket/FWSocketHandle.ts +14 -14
- package/service/socket/FWSocketSender.ts +11 -12
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { FWSystemDefine } from
|
|
2
|
-
import FWLog from
|
|
3
|
-
import { FWManager } from
|
|
4
|
-
|
|
5
|
-
export default class FWPromiseManager
|
|
1
|
+
import { FWSystemDefine } from "../define/FWSystemDefine";
|
|
2
|
+
import FWLog from "../log/FWLog";
|
|
3
|
+
import { FWManager } from "./FWManager";
|
|
4
|
+
|
|
5
|
+
export default class FWPromiseManager
|
|
6
|
+
extends FWManager
|
|
7
|
+
implements FW.PromiseManager
|
|
8
|
+
{
|
|
6
9
|
private promiseRegistry: Map<number, FW.PromiseProxy>;
|
|
7
10
|
private uniqueId: number = 0;
|
|
8
11
|
|
|
@@ -18,7 +21,7 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
18
21
|
/** 创建Promise执行器 */
|
|
19
22
|
public execute<T = any>(
|
|
20
23
|
executor: FW.PromiseExcutor<T>,
|
|
21
|
-
options: FW.PromiseExecuteOptions = {}
|
|
24
|
+
options: FW.PromiseExecuteOptions = {}
|
|
22
25
|
): FW.PromiseProxy<T> {
|
|
23
26
|
const id = this.uniqueId++;
|
|
24
27
|
const abortController = new AbortController();
|
|
@@ -30,13 +33,18 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
30
33
|
return new Promise<T>((resolve, reject) => {
|
|
31
34
|
if (options.timeout && options.timeout > 0) {
|
|
32
35
|
timerSchedule = FW.Entry.timeMgr.scheduleOnce(() => {
|
|
33
|
-
const timeoutError = new Error(
|
|
36
|
+
const timeoutError = new Error(
|
|
37
|
+
`Promise ${id} timeout after ${options.timeout} s`
|
|
38
|
+
);
|
|
34
39
|
if (
|
|
35
40
|
retryCount < maxRetryTimes &&
|
|
36
|
-
(!options.retryCondition ||
|
|
41
|
+
(!options.retryCondition ||
|
|
42
|
+
options.retryCondition(timeoutError, retryCount))
|
|
37
43
|
) {
|
|
38
44
|
retryCount++;
|
|
39
|
-
FWLog.debug(
|
|
45
|
+
FWLog.debug(
|
|
46
|
+
`Promise ${id} timeout, retrying (${retryCount}/${maxRetryTimes})`
|
|
47
|
+
);
|
|
40
48
|
if (retryInterval > 0) {
|
|
41
49
|
FW.Entry.timeMgr.scheduleOnce(() => {
|
|
42
50
|
createPromise().then(resolve, reject);
|
|
@@ -64,11 +72,11 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
64
72
|
return;
|
|
65
73
|
}
|
|
66
74
|
|
|
67
|
-
abortController.signal.addEventListener(
|
|
75
|
+
abortController.signal.addEventListener("abort", onAbort);
|
|
68
76
|
|
|
69
77
|
const wrappedResolve = (value: T | PromiseLike<T>) => {
|
|
70
78
|
promiseProxy.status = FWSystemDefine.FWPromiseStatus.FULFILLED;
|
|
71
|
-
abortController.signal.removeEventListener(
|
|
79
|
+
abortController.signal.removeEventListener("abort", onAbort);
|
|
72
80
|
this.removePromise(id);
|
|
73
81
|
timerSchedule?.unSchedule();
|
|
74
82
|
resolve(value);
|
|
@@ -78,10 +86,14 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
78
86
|
timerSchedule?.unSchedule();
|
|
79
87
|
if (
|
|
80
88
|
retryCount < maxRetryTimes &&
|
|
81
|
-
(!options.retryCondition ||
|
|
89
|
+
(!options.retryCondition ||
|
|
90
|
+
options.retryCondition(reason, retryCount))
|
|
82
91
|
) {
|
|
83
92
|
retryCount++;
|
|
84
|
-
FWLog.debug(
|
|
93
|
+
FWLog.debug(
|
|
94
|
+
`Promise ${id} failed, retrying (${retryCount}/${maxRetryTimes}):`,
|
|
95
|
+
reason
|
|
96
|
+
);
|
|
85
97
|
if (retryInterval > 0) {
|
|
86
98
|
FW.Entry.timeMgr.scheduleOnce(() => {
|
|
87
99
|
createPromise().then(resolve, reject);
|
|
@@ -90,16 +102,23 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
90
102
|
createPromise().then(resolve, reject);
|
|
91
103
|
}
|
|
92
104
|
} else {
|
|
93
|
-
if (
|
|
105
|
+
if (
|
|
106
|
+
promiseProxy.status === FWSystemDefine.FWPromiseStatus.PENDING
|
|
107
|
+
) {
|
|
94
108
|
promiseProxy.status = FWSystemDefine.FWPromiseStatus.REJECTED;
|
|
95
|
-
abortController.signal.removeEventListener(
|
|
109
|
+
abortController.signal.removeEventListener("abort", onAbort);
|
|
96
110
|
this.removePromise(id);
|
|
97
111
|
reject(reason);
|
|
98
112
|
}
|
|
99
113
|
}
|
|
100
114
|
};
|
|
101
115
|
try {
|
|
102
|
-
executor(
|
|
116
|
+
executor(
|
|
117
|
+
wrappedResolve,
|
|
118
|
+
wrappedReject,
|
|
119
|
+
abortController.signal,
|
|
120
|
+
options.reason
|
|
121
|
+
);
|
|
103
122
|
} catch (error) {
|
|
104
123
|
wrappedReject(error);
|
|
105
124
|
}
|
|
@@ -114,16 +133,16 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
114
133
|
abortController,
|
|
115
134
|
abort: (reason?: any) => {
|
|
116
135
|
if (promiseProxy.status === FWSystemDefine.FWPromiseStatus.PENDING) {
|
|
117
|
-
FWLog.debug(reason ||
|
|
136
|
+
FWLog.debug(reason || "promise cancelled");
|
|
118
137
|
abortController.abort(reason);
|
|
119
138
|
}
|
|
120
139
|
},
|
|
121
140
|
|
|
122
141
|
addAbortEventListener: (
|
|
123
142
|
listener: (this: AbortSignal, ev: Event) => any,
|
|
124
|
-
options?: boolean | AddEventListenerOptions
|
|
143
|
+
options?: boolean | AddEventListenerOptions
|
|
125
144
|
) => {
|
|
126
|
-
abortController.signal.addEventListener(
|
|
145
|
+
abortController.signal.addEventListener("abort", listener, options);
|
|
127
146
|
},
|
|
128
147
|
};
|
|
129
148
|
|
|
@@ -134,7 +153,7 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
134
153
|
/** 批量执行Promise并等待所有完成 */
|
|
135
154
|
public all<T = any>(
|
|
136
155
|
promises: FW.PromiseProxy<T>[],
|
|
137
|
-
options: FW.PromiseExecuteOptions = {}
|
|
156
|
+
options: FW.PromiseExecuteOptions = {}
|
|
138
157
|
): FW.PromiseProxy<FW.PromiseResult<T>> {
|
|
139
158
|
const id = this.uniqueId++;
|
|
140
159
|
const abortController = new AbortController();
|
|
@@ -148,13 +167,18 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
148
167
|
if (options.timeout && options.timeout > 0) {
|
|
149
168
|
timerSchedule?.unSchedule();
|
|
150
169
|
timerSchedule = FW.Entry.timeMgr.scheduleOnce(() => {
|
|
151
|
-
const timeoutError = new Error(
|
|
170
|
+
const timeoutError = new Error(
|
|
171
|
+
`All Promise ${id} timeout after ${options.timeout} s`
|
|
172
|
+
);
|
|
152
173
|
if (
|
|
153
174
|
retryCount < maxRetryTimes &&
|
|
154
|
-
(!options.retryCondition ||
|
|
175
|
+
(!options.retryCondition ||
|
|
176
|
+
options.retryCondition(timeoutError, retryCount))
|
|
155
177
|
) {
|
|
156
178
|
retryCount++;
|
|
157
|
-
FWLog.debug(
|
|
179
|
+
FWLog.debug(
|
|
180
|
+
`All Promise ${id} timeout, retrying (${retryCount}/${maxRetryTimes})`
|
|
181
|
+
);
|
|
158
182
|
if (retryInterval > 0) {
|
|
159
183
|
FW.Entry.timeMgr.scheduleOnce(() => {
|
|
160
184
|
createPromise().then(resolve, reject);
|
|
@@ -182,7 +206,7 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
182
206
|
return;
|
|
183
207
|
}
|
|
184
208
|
|
|
185
|
-
abortController.signal.addEventListener(
|
|
209
|
+
abortController.signal.addEventListener("abort", onAbort);
|
|
186
210
|
|
|
187
211
|
const processAll = async () => {
|
|
188
212
|
const result: FW.PromiseResult<T> = {
|
|
@@ -199,8 +223,8 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
199
223
|
.filter(
|
|
200
224
|
(id) =>
|
|
201
225
|
!result.success.some((s) => s.id === id) &&
|
|
202
|
-
!result.failed.some((f) => f.id === id)
|
|
203
|
-
)
|
|
226
|
+
!result.failed.some((f) => f.id === id)
|
|
227
|
+
)
|
|
204
228
|
);
|
|
205
229
|
break;
|
|
206
230
|
}
|
|
@@ -209,7 +233,9 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
209
233
|
const value = await promiseProxy.promise;
|
|
210
234
|
result.success.push(value);
|
|
211
235
|
} catch (error) {
|
|
212
|
-
if (
|
|
236
|
+
if (
|
|
237
|
+
promiseProxy.status === FWSystemDefine.FWPromiseStatus.CANCELLED
|
|
238
|
+
) {
|
|
213
239
|
result.cancelled.push(promiseProxy.id);
|
|
214
240
|
} else {
|
|
215
241
|
result.failed.push({
|
|
@@ -226,7 +252,7 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
226
252
|
processAll()
|
|
227
253
|
.then((result) => {
|
|
228
254
|
promiseProxy.status = FWSystemDefine.FWPromiseStatus.FULFILLED;
|
|
229
|
-
abortController.signal.removeEventListener(
|
|
255
|
+
abortController.signal.removeEventListener("abort", onAbort);
|
|
230
256
|
this.removePromise(id);
|
|
231
257
|
timerSchedule?.unSchedule();
|
|
232
258
|
resolve(result);
|
|
@@ -235,12 +261,13 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
235
261
|
timerSchedule?.unSchedule();
|
|
236
262
|
if (
|
|
237
263
|
retryCount < maxRetryTimes &&
|
|
238
|
-
(!options.retryCondition ||
|
|
264
|
+
(!options.retryCondition ||
|
|
265
|
+
options.retryCondition(error, retryCount))
|
|
239
266
|
) {
|
|
240
267
|
retryCount++;
|
|
241
268
|
FWLog.debug(
|
|
242
269
|
`All Promise ${id} failed, retrying (${retryCount}/${maxRetryTimes}):`,
|
|
243
|
-
error
|
|
270
|
+
error
|
|
244
271
|
);
|
|
245
272
|
if (retryInterval > 0) {
|
|
246
273
|
FW.Entry.timeMgr.scheduleOnce(() => {
|
|
@@ -250,9 +277,11 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
250
277
|
createPromise().then(resolve, reject);
|
|
251
278
|
}
|
|
252
279
|
} else {
|
|
253
|
-
if (
|
|
280
|
+
if (
|
|
281
|
+
promiseProxy.status === FWSystemDefine.FWPromiseStatus.PENDING
|
|
282
|
+
) {
|
|
254
283
|
promiseProxy.status = FWSystemDefine.FWPromiseStatus.REJECTED;
|
|
255
|
-
abortController.signal.removeEventListener(
|
|
284
|
+
abortController.signal.removeEventListener("abort", onAbort);
|
|
256
285
|
this.removePromise(id);
|
|
257
286
|
reject(error);
|
|
258
287
|
}
|
|
@@ -269,15 +298,15 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
269
298
|
abortController,
|
|
270
299
|
abort: (reason?: any) => {
|
|
271
300
|
if (promiseProxy.status === FWSystemDefine.FWPromiseStatus.PENDING) {
|
|
272
|
-
FWLog.debug(reason ||
|
|
301
|
+
FWLog.debug(reason || "all promise cancelled");
|
|
273
302
|
abortController.abort(reason);
|
|
274
303
|
}
|
|
275
304
|
},
|
|
276
305
|
addAbortEventListener: (
|
|
277
306
|
listener: (this: AbortSignal, ev: Event) => any,
|
|
278
|
-
options?: boolean | AddEventListenerOptions
|
|
307
|
+
options?: boolean | AddEventListenerOptions
|
|
279
308
|
) => {
|
|
280
|
-
abortController.signal.addEventListener(
|
|
309
|
+
abortController.signal.addEventListener("abort", listener, options);
|
|
281
310
|
},
|
|
282
311
|
};
|
|
283
312
|
|
|
@@ -288,7 +317,10 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
288
317
|
/** 取消指定Promise */
|
|
289
318
|
public cancel(id: number, reason?: any): boolean {
|
|
290
319
|
const promiseProxy = this.promiseRegistry.get(id);
|
|
291
|
-
if (
|
|
320
|
+
if (
|
|
321
|
+
promiseProxy &&
|
|
322
|
+
promiseProxy.status === FWSystemDefine.FWPromiseStatus.PENDING
|
|
323
|
+
) {
|
|
292
324
|
promiseProxy.abort(reason);
|
|
293
325
|
return true;
|
|
294
326
|
}
|
|
@@ -313,7 +345,6 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
313
345
|
promiseProxy.abort(reason);
|
|
314
346
|
cancelled.push(id);
|
|
315
347
|
});
|
|
316
|
-
FWLog.error(cancelled);
|
|
317
348
|
return cancelled;
|
|
318
349
|
}
|
|
319
350
|
|
|
@@ -340,7 +371,7 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
340
371
|
/** 获取正在执行的Promise数量 */
|
|
341
372
|
public getActiveCount(): number {
|
|
342
373
|
return Array.from(this.promiseRegistry.values()).filter(
|
|
343
|
-
(p) => p.status === FWSystemDefine.FWPromiseStatus.PENDING
|
|
374
|
+
(p) => p.status === FWSystemDefine.FWPromiseStatus.PENDING
|
|
344
375
|
).length;
|
|
345
376
|
}
|
|
346
377
|
|
package/manager/FWTaskManager.ts
CHANGED
|
@@ -22,14 +22,14 @@ export default class FWTaskManager extends FWManager implements FW.TaskManager {
|
|
|
22
22
|
taskId: options?.taskId || ++this.taskId,
|
|
23
23
|
log: options?.log,
|
|
24
24
|
});
|
|
25
|
-
this.tasks.set(task.id, task);
|
|
25
|
+
this.tasks.set(task.id, task);
|
|
26
26
|
return task;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
getTask(taskId: number): FW.Task | undefined {
|
|
30
30
|
return this.tasks.get(taskId);
|
|
31
31
|
}
|
|
32
|
-
|
|
32
|
+
|
|
33
33
|
stopTask(taskId: number): void {
|
|
34
34
|
this.tasks.get(taskId)?.stop();
|
|
35
35
|
this.tasks.delete(taskId);
|
package/package.json
CHANGED
package/service/FWService.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import { FrameworkBase } from
|
|
1
|
+
import { FrameworkBase } from "../FrameworkBase";
|
|
2
2
|
|
|
3
3
|
export default abstract class FWService extends FrameworkBase {
|
|
4
|
-
public initialize()
|
|
5
|
-
this.dependencies();
|
|
6
|
-
}
|
|
4
|
+
public abstract initialize();
|
|
7
5
|
public abstract onDestroy(): void;
|
|
8
6
|
|
|
9
7
|
protected onRestart() {
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import { FWSystemConfig } from
|
|
2
|
-
import FWLog from
|
|
3
|
-
import FWService from
|
|
4
|
-
import FWSocketHandle from './FWSocketHandle';
|
|
5
|
-
import FWSocketSender from './FWSocketSender';
|
|
1
|
+
import { FWSystemConfig } from "../../config/FWSystemConfig";
|
|
2
|
+
import FWLog from "../../log/FWLog";
|
|
3
|
+
import FWService from "../FWService";
|
|
6
4
|
|
|
7
5
|
/**
|
|
8
6
|
* TODO 犹豫socket没有改版暂时已老的cmd字符串映射方式做,后期优化
|
|
@@ -15,9 +13,9 @@ export default class FWSocket extends FWService implements FW.Socket {
|
|
|
15
13
|
/** socket */
|
|
16
14
|
socket: WebSocket = null;
|
|
17
15
|
/** 消息发送者 */
|
|
18
|
-
protected socketSender:
|
|
16
|
+
protected socketSender: FW.SocketSender;
|
|
19
17
|
/** 消息处理者 */
|
|
20
|
-
protected socketHandle:
|
|
18
|
+
protected socketHandle: FW.SocketHandle;
|
|
21
19
|
/** 重连次数 */
|
|
22
20
|
protected reconnectTimes: number;
|
|
23
21
|
/** 心跳间隔时间 */
|
|
@@ -56,7 +54,6 @@ export default class FWSocket extends FWService implements FW.Socket {
|
|
|
56
54
|
protected messageEvents: { [key: string]: (msg: any) => void };
|
|
57
55
|
|
|
58
56
|
public initialize(): void {
|
|
59
|
-
super.initialize();
|
|
60
57
|
this.messageEvents = cc.js.createMap();
|
|
61
58
|
this.protocolContainer = new Map<Symbol, FW.ProtocolPolling>();
|
|
62
59
|
this.protocolRegistry = new Map<string, Set<Symbol>>();
|
|
@@ -68,7 +65,7 @@ export default class FWSocket extends FWService implements FW.Socket {
|
|
|
68
65
|
tag: string,
|
|
69
66
|
socketSender: FW.SocketSender,
|
|
70
67
|
socketHandle: FW.SocketHandle,
|
|
71
|
-
config: FW.SocketConfig
|
|
68
|
+
config: FW.SocketConfig
|
|
72
69
|
): FW.SocketPromiseProxy {
|
|
73
70
|
try {
|
|
74
71
|
FW.Entry.timeMgr.unSchedule(this);
|
|
@@ -112,10 +109,14 @@ export default class FWSocket extends FWService implements FW.Socket {
|
|
|
112
109
|
this.heartInternal = config?.heartInternal || defaultConfig.heartInternal;
|
|
113
110
|
this.heartTimeout = config?.heartTimeout || defaultConfig.heartTimeout;
|
|
114
111
|
this.heartWeakTime = config?.heartWeakTime || defaultConfig.heartWeakTime;
|
|
115
|
-
this.maxReconnectTimes =
|
|
116
|
-
|
|
117
|
-
this.
|
|
118
|
-
|
|
112
|
+
this.maxReconnectTimes =
|
|
113
|
+
config?.maxReconnectTimes || defaultConfig.maxReconnectTimes;
|
|
114
|
+
this.reconnectInternal =
|
|
115
|
+
config?.reconnectInternal || defaultConfig.reconnectInternal;
|
|
116
|
+
this.protocolSymbol =
|
|
117
|
+
config?.protocolSymbol || defaultConfig.protocolSymbol;
|
|
118
|
+
this.protocolPollingTime =
|
|
119
|
+
config?.protocolPollingTime || defaultConfig.protocolPollingTime;
|
|
119
120
|
|
|
120
121
|
this.promiseProxy = {
|
|
121
122
|
promise: undefined,
|
|
@@ -129,7 +130,7 @@ export default class FWSocket extends FWService implements FW.Socket {
|
|
|
129
130
|
|
|
130
131
|
return this.promiseProxy;
|
|
131
132
|
} catch (e) {
|
|
132
|
-
FWLog.error(
|
|
133
|
+
FWLog.error("创建socket失败:", e);
|
|
133
134
|
}
|
|
134
135
|
}
|
|
135
136
|
/**
|
|
@@ -195,7 +196,7 @@ export default class FWSocket extends FWService implements FW.Socket {
|
|
|
195
196
|
if (force) this.reconnectTimes = 0;
|
|
196
197
|
|
|
197
198
|
FWLog.debug(
|
|
198
|
-
`socket reconnect : reconnectTimes->${this.reconnectTimes},maxReconnectTimes->${this.maxReconnectTimes}
|
|
199
|
+
`socket reconnect : reconnectTimes->${this.reconnectTimes},maxReconnectTimes->${this.maxReconnectTimes}`
|
|
199
200
|
);
|
|
200
201
|
if (++this.reconnectTimes >= this.maxReconnectTimes) {
|
|
201
202
|
this.socketHandle?.onTimeout?.();
|
|
@@ -206,16 +207,22 @@ export default class FWSocket extends FWService implements FW.Socket {
|
|
|
206
207
|
this.socket?.close();
|
|
207
208
|
this.socket = null;
|
|
208
209
|
|
|
209
|
-
this.createSocket(
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
210
|
+
this.createSocket(
|
|
211
|
+
this.address,
|
|
212
|
+
this.tag,
|
|
213
|
+
this.socketSender,
|
|
214
|
+
this.socketHandle,
|
|
215
|
+
{
|
|
216
|
+
heartInternal: this.heartInternal,
|
|
217
|
+
heartTimeout: this.heartTimeout,
|
|
218
|
+
heartWeakTime: this.heartWeakTime,
|
|
219
|
+
maxReconnectTimes: this.maxReconnectTimes,
|
|
220
|
+
reconnectInternal: this.reconnectInternal,
|
|
221
|
+
protocolSymbol: this.protocolSymbol,
|
|
222
|
+
protocolPollingTime: this.protocolPollingTime,
|
|
223
|
+
certificate: this.certificate,
|
|
224
|
+
}
|
|
225
|
+
);
|
|
219
226
|
}
|
|
220
227
|
|
|
221
228
|
async send(msg: string) {
|
|
@@ -225,7 +232,8 @@ export default class FWSocket extends FWService implements FW.Socket {
|
|
|
225
232
|
const protocolKey = this.socketSender.getProtocolKey?.(msg);
|
|
226
233
|
if (protocolKey) {
|
|
227
234
|
const symbol = Symbol(protocolKey);
|
|
228
|
-
const registry =
|
|
235
|
+
const registry =
|
|
236
|
+
this.protocolRegistry.get(protocolKey) || new Set<Symbol>();
|
|
229
237
|
const protocolPolling: FW.ProtocolPolling = {
|
|
230
238
|
msg: msg,
|
|
231
239
|
schedule: FW.Entry.timeMgr.schedule(
|
|
@@ -234,7 +242,7 @@ export default class FWSocket extends FWService implements FW.Socket {
|
|
|
234
242
|
},
|
|
235
243
|
this.protocolPollingTime,
|
|
236
244
|
cc.macro.REPEAT_FOREVER,
|
|
237
|
-
this
|
|
245
|
+
this
|
|
238
246
|
),
|
|
239
247
|
};
|
|
240
248
|
this.protocolRegistry.set(protocolKey, registry.add(symbol));
|
|
@@ -246,7 +254,7 @@ export default class FWSocket extends FWService implements FW.Socket {
|
|
|
246
254
|
|
|
247
255
|
/** 连接打开 */
|
|
248
256
|
private onSocketOpen() {
|
|
249
|
-
FWLog.debug(
|
|
257
|
+
FWLog.debug("on open!");
|
|
250
258
|
FW.Entry.timeMgr.unSchedule(this);
|
|
251
259
|
this.reconnectTimes = 0;
|
|
252
260
|
this.sendHeartTimestamp = 0;
|
|
@@ -271,7 +279,7 @@ export default class FWSocket extends FWService implements FW.Socket {
|
|
|
271
279
|
},
|
|
272
280
|
this.heartInternal / 1000,
|
|
273
281
|
cc.macro.REPEAT_FOREVER,
|
|
274
|
-
this
|
|
282
|
+
this
|
|
275
283
|
);
|
|
276
284
|
}
|
|
277
285
|
|
|
@@ -290,17 +298,20 @@ export default class FWSocket extends FWService implements FW.Socket {
|
|
|
290
298
|
|
|
291
299
|
/** socket关闭 */
|
|
292
300
|
private onSocketClose() {
|
|
293
|
-
FWLog.debug(
|
|
301
|
+
FWLog.debug("on close!");
|
|
294
302
|
this.socketHandle?.onClose?.();
|
|
295
303
|
FW.Entry.timeMgr.scheduleOnce(
|
|
296
304
|
() => {
|
|
297
|
-
if (
|
|
298
|
-
|
|
305
|
+
if (
|
|
306
|
+
this.getReadyState() == WebSocket.CLOSING ||
|
|
307
|
+
this.getReadyState() == WebSocket.CLOSED
|
|
308
|
+
) {
|
|
309
|
+
FWLog.debug("on close!");
|
|
299
310
|
this.reconnect();
|
|
300
311
|
}
|
|
301
312
|
},
|
|
302
313
|
this.reconnectInternal,
|
|
303
|
-
this
|
|
314
|
+
this
|
|
304
315
|
);
|
|
305
316
|
}
|
|
306
317
|
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { injectable } from
|
|
1
|
+
import { injectable } from "inversify";
|
|
2
|
+
import { FrameworkBase } from "../../FrameworkBase";
|
|
2
3
|
@injectable()
|
|
3
|
-
export default abstract class FWSocketHandle
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
public readonly entry: FW.Entry = FW.Entry;
|
|
8
|
-
abstract onMessage(msg: any): void;
|
|
4
|
+
export default abstract class FWSocketHandle
|
|
5
|
+
extends FrameworkBase
|
|
6
|
+
implements FW.SocketHandle
|
|
7
|
+
{
|
|
9
8
|
initialize?();
|
|
10
9
|
onDestroy?();
|
|
11
|
-
onHeart?(msg: any): Promise<boolean>;
|
|
12
10
|
onWeakNetWork?(): void;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
abstract onMessage(msg: any): void;
|
|
12
|
+
abstract onHeart?(msg: any): Promise<boolean>;
|
|
13
|
+
abstract onClose?(): void;
|
|
14
|
+
abstract onOpen?(): void;
|
|
15
|
+
abstract onError?(msg: any): void;
|
|
16
|
+
abstract onTimeout?(): void;
|
|
17
|
+
abstract getProtocolKey?(msg: any): string;
|
|
18
|
+
abstract onBeforeReceivingMessage?(msg: any): Promise<FW.SocketMessage>;
|
|
19
19
|
}
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import { injectable } from
|
|
2
|
-
import FWLog from
|
|
1
|
+
import { injectable } from "inversify";
|
|
2
|
+
import FWLog from "../../log/FWLog";
|
|
3
|
+
import { FrameworkBase } from "../../FrameworkBase";
|
|
3
4
|
@injectable()
|
|
4
|
-
export default class FWSocketSender
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
public readonly entry: FW.Entry = FW.Entry;
|
|
5
|
+
export default abstract class FWSocketSender
|
|
6
|
+
extends FrameworkBase
|
|
7
|
+
implements FW.SocketSender
|
|
8
|
+
{
|
|
9
9
|
initialize?();
|
|
10
10
|
onDestroy?();
|
|
11
|
-
sendHeart
|
|
12
|
-
onHeart?(msg: any): Promise<boolean>;
|
|
13
|
-
onBeforeSendingMessage?(msg: any): Promise<FW.SocketMessage>;
|
|
14
|
-
getProtocolKey?(msg: any): string;
|
|
15
|
-
|
|
11
|
+
abstract sendHeart();
|
|
12
|
+
abstract onHeart?(msg: any): Promise<boolean>;
|
|
13
|
+
abstract onBeforeSendingMessage?(msg: any): Promise<FW.SocketMessage>;
|
|
14
|
+
abstract getProtocolKey?(msg: any): string;
|
|
16
15
|
send(msg: any) {
|
|
17
16
|
FW.Entry.socketMgr.getSocket()?.send(msg);
|
|
18
17
|
}
|