@ives_xxz/framework 1.0.4 → 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/package.json +1 -1
- package/service/socket/FWSocket.ts +36 -22
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { FWSystemConfig } from
|
|
2
|
-
import FWLog from
|
|
3
|
-
import FWSocketHandle from
|
|
4
|
-
import FWSocketSender from
|
|
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字符串映射方式做,后期优化
|
|
@@ -67,7 +67,7 @@ export default class FWSocket implements FW.Socket {
|
|
|
67
67
|
tag: string,
|
|
68
68
|
sender: FW.SocketSender,
|
|
69
69
|
handle: FW.SocketHandle,
|
|
70
|
-
config: FW.SocketConfig
|
|
70
|
+
config: FW.SocketConfig
|
|
71
71
|
): FW.SocketPromiseProxy {
|
|
72
72
|
FW.Entry.timeMgr.unSchedule(this);
|
|
73
73
|
|
|
@@ -94,9 +94,12 @@ export default class FWSocket implements FW.Socket {
|
|
|
94
94
|
this.heartInternal = config?.heartInternal || defaultConfig.heartInternal;
|
|
95
95
|
this.heartTimeout = config?.heartTimeout || defaultConfig.heartTimeout;
|
|
96
96
|
this.heartWeakTime = config?.heartWeakTime || defaultConfig.heartWeakTime;
|
|
97
|
-
this.maxReconnectTimes =
|
|
98
|
-
|
|
99
|
-
this.
|
|
97
|
+
this.maxReconnectTimes =
|
|
98
|
+
config?.maxReconnectTimes || defaultConfig.maxReconnectTimes;
|
|
99
|
+
this.reconnectInternal =
|
|
100
|
+
config?.reconnectInternal || defaultConfig.reconnectInternal;
|
|
101
|
+
this.protocolSymbol =
|
|
102
|
+
config?.protocolSymbol || defaultConfig.protocolSymbol;
|
|
100
103
|
|
|
101
104
|
this.promiseProxy = {
|
|
102
105
|
promise: undefined,
|
|
@@ -162,7 +165,7 @@ export default class FWSocket implements FW.Socket {
|
|
|
162
165
|
}
|
|
163
166
|
|
|
164
167
|
FWLog.debug(
|
|
165
|
-
`socket reconnect : reconnectTimes->${this.reconnectTimes},maxReconnectTimes->${this.maxReconnectTimes}
|
|
168
|
+
`socket reconnect : reconnectTimes->${this.reconnectTimes},maxReconnectTimes->${this.maxReconnectTimes}`
|
|
166
169
|
);
|
|
167
170
|
if (++this.reconnectTimes >= this.maxReconnectTimes) {
|
|
168
171
|
this.handle?.onTimeout?.();
|
|
@@ -189,7 +192,8 @@ export default class FWSocket implements FW.Socket {
|
|
|
189
192
|
const protocolKey = this.sender.getProtocolKey?.(msg);
|
|
190
193
|
if (protocolKey) {
|
|
191
194
|
const symbol = Symbol(protocolKey);
|
|
192
|
-
const registry =
|
|
195
|
+
const registry =
|
|
196
|
+
this.protocolRegistry.get(protocolKey) || new Set<Symbol>();
|
|
193
197
|
this.protocolRegistry.set(protocolKey, registry.add(symbol));
|
|
194
198
|
this.protocolContainer.set(symbol, msg);
|
|
195
199
|
}
|
|
@@ -199,7 +203,7 @@ export default class FWSocket implements FW.Socket {
|
|
|
199
203
|
|
|
200
204
|
/** 连接打开 */
|
|
201
205
|
private onSocketOpen() {
|
|
202
|
-
FWLog.debug(
|
|
206
|
+
FWLog.debug("on open!");
|
|
203
207
|
FW.Entry.timeMgr.unSchedule(this);
|
|
204
208
|
this.reconnectTimes = 0;
|
|
205
209
|
this.sendHeartTimestamp = 0;
|
|
@@ -231,7 +235,7 @@ export default class FWSocket implements FW.Socket {
|
|
|
231
235
|
},
|
|
232
236
|
this.heartInternal / 1000,
|
|
233
237
|
cc.macro.REPEAT_FOREVER,
|
|
234
|
-
this
|
|
238
|
+
this
|
|
235
239
|
);
|
|
236
240
|
}
|
|
237
241
|
|
|
@@ -241,7 +245,11 @@ export default class FWSocket implements FW.Socket {
|
|
|
241
245
|
}
|
|
242
246
|
|
|
243
247
|
private sendMessage(msg: any) {
|
|
244
|
-
|
|
248
|
+
try {
|
|
249
|
+
this.socket?.send(this.sender.onBeforeSendingMessage?.(msg) || msg);
|
|
250
|
+
} catch (e) {
|
|
251
|
+
this.socket?.send(msg);
|
|
252
|
+
}
|
|
245
253
|
}
|
|
246
254
|
|
|
247
255
|
/** socket关闭 */
|
|
@@ -250,11 +258,11 @@ export default class FWSocket implements FW.Socket {
|
|
|
250
258
|
FW.Entry.timeMgr.scheduleOnce(
|
|
251
259
|
() => {
|
|
252
260
|
if (!this.isReconnect) return;
|
|
253
|
-
FWLog.debug(
|
|
261
|
+
FWLog.debug("on close!");
|
|
254
262
|
this.reconnect();
|
|
255
263
|
},
|
|
256
264
|
this.reconnectInternal,
|
|
257
|
-
this
|
|
265
|
+
this
|
|
258
266
|
);
|
|
259
267
|
}
|
|
260
268
|
/** 消息处理 */
|
|
@@ -266,6 +274,19 @@ export default class FWSocket implements FW.Socket {
|
|
|
266
274
|
return;
|
|
267
275
|
}
|
|
268
276
|
|
|
277
|
+
this.remoteProtocol(msg);
|
|
278
|
+
|
|
279
|
+
if (this.isPausedMessageHandle) return;
|
|
280
|
+
|
|
281
|
+
this.handle?.onMessage?.(msg);
|
|
282
|
+
}
|
|
283
|
+
/** 错误处理 */
|
|
284
|
+
private async onSocketError(msg: any) {
|
|
285
|
+
await this.remoteProtocol(msg);
|
|
286
|
+
this.handle?.onError?.(msg);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
private async remoteProtocol(msg: any) {
|
|
269
290
|
const protocolKey = await this.handle.getProtocolKey?.(msg);
|
|
270
291
|
if (protocolKey && this.protocolRegistry.has(protocolKey)) {
|
|
271
292
|
const symbolSet = this.protocolRegistry.get(protocolKey);
|
|
@@ -282,12 +303,5 @@ export default class FWSocket implements FW.Socket {
|
|
|
282
303
|
}
|
|
283
304
|
}
|
|
284
305
|
}
|
|
285
|
-
|
|
286
|
-
if (this.isPausedMessageHandle) return;
|
|
287
|
-
this.handle?.onMessage?.(msg);
|
|
288
|
-
}
|
|
289
|
-
/** 错误处理 */
|
|
290
|
-
private onSocketError(msg: any) {
|
|
291
|
-
this.handle?.onError?.(msg);
|
|
292
306
|
}
|
|
293
307
|
}
|