@kkarum/framework 2.3.17

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.
Files changed (171) hide show
  1. package/Framework.ts +337 -0
  2. package/Framework.ts.meta +10 -0
  3. package/FrameworkBase.ts +207 -0
  4. package/FrameworkBase.ts.meta +10 -0
  5. package/FrameworkInitialize.ts +79 -0
  6. package/FrameworkInitialize.ts.meta +10 -0
  7. package/README.md +93 -0
  8. package/README.md.meta +6 -0
  9. package/animation/FWAnimation.ts +13 -0
  10. package/animation/FWAnimation.ts.meta +10 -0
  11. package/animation/FWSkeleton.ts +174 -0
  12. package/animation/FWSkeleton.ts.meta +10 -0
  13. package/animation/FWTween.ts +60 -0
  14. package/animation/FWTween.ts.meta +10 -0
  15. package/animation.meta +13 -0
  16. package/component/FWVirtuaScrollViewComponent.ts +56 -0
  17. package/component/FWVirtuaScrollViewComponent.ts.meta +10 -0
  18. package/component/FWVirtualViewComponent.ts +2212 -0
  19. package/component/FWVirtualViewComponent.ts.meta +10 -0
  20. package/component.meta +13 -0
  21. package/config/FWAssetConfig.ts +7 -0
  22. package/config/FWAssetConfig.ts.meta +10 -0
  23. package/config/FWSystemConfig.ts +29 -0
  24. package/config/FWSystemConfig.ts.meta +10 -0
  25. package/config.meta +13 -0
  26. package/controller/FWLayerController.ts +178 -0
  27. package/controller/FWLayerController.ts.meta +10 -0
  28. package/controller.meta +13 -0
  29. package/data/FWData.ts +6 -0
  30. package/data/FWData.ts.meta +10 -0
  31. package/data.meta +13 -0
  32. package/define/FWEventDefine.ts +28 -0
  33. package/define/FWEventDefine.ts.meta +10 -0
  34. package/define/FWSystemDefine.ts +154 -0
  35. package/define/FWSystemDefine.ts.meta +10 -0
  36. package/define.meta +13 -0
  37. package/entry/FWEntry.ts +219 -0
  38. package/entry/FWEntry.ts.meta +10 -0
  39. package/entry.meta +13 -0
  40. package/expand/FWCocosExpand.ts +73 -0
  41. package/expand/FWCocosExpand.ts.meta +10 -0
  42. package/expand/FWDecorator.ts +366 -0
  43. package/expand/FWDecorator.ts.meta +10 -0
  44. package/expand/FWNodeExpand.ts +147 -0
  45. package/expand/FWNodeExpand.ts.meta +10 -0
  46. package/expand/FWRollingViewNesting.ts +195 -0
  47. package/expand/FWRollingViewNesting.ts.meta +10 -0
  48. package/expand/FWTweenExpand.ts +44 -0
  49. package/expand/FWTweenExpand.ts.meta +10 -0
  50. package/expand.meta +13 -0
  51. package/item/FWVirtualListItem.ts +141 -0
  52. package/item/FWVirtualListItem.ts.meta +10 -0
  53. package/item.meta +13 -0
  54. package/language/FWLanguage.ts +71 -0
  55. package/language/FWLanguage.ts.meta +10 -0
  56. package/language/FWLanguageLabelLocalize.ts +144 -0
  57. package/language/FWLanguageLabelLocalize.ts.meta +10 -0
  58. package/language/FWLanguageSkeletonLocalize.ts +106 -0
  59. package/language/FWLanguageSkeletonLocalize.ts.meta +10 -0
  60. package/language/FWLanguageSpriteLocalize.ts +81 -0
  61. package/language/FWLanguageSpriteLocalize.ts.meta +10 -0
  62. package/language.meta +13 -0
  63. package/layer/FWLayer.ts +14 -0
  64. package/layer/FWLayer.ts.meta +10 -0
  65. package/layer.meta +13 -0
  66. package/log/FWLog.ts +135 -0
  67. package/log/FWLog.ts.meta +10 -0
  68. package/log.meta +13 -0
  69. package/logic/FWLogic.ts +6 -0
  70. package/logic/FWLogic.ts.meta +10 -0
  71. package/logic.meta +13 -0
  72. package/machine/FWAnimationMachine.ts +28 -0
  73. package/machine/FWAnimationMachine.ts.meta +10 -0
  74. package/machine/FWStateMachine.ts +74 -0
  75. package/machine/FWStateMachine.ts.meta +10 -0
  76. package/machine.meta +13 -0
  77. package/manager/FWAnimationManager.ts +89 -0
  78. package/manager/FWAnimationManager.ts.meta +10 -0
  79. package/manager/FWAssetManager.ts +451 -0
  80. package/manager/FWAssetManager.ts.meta +10 -0
  81. package/manager/FWAudioManager.ts +339 -0
  82. package/manager/FWAudioManager.ts.meta +10 -0
  83. package/manager/FWBundleManager.ts +97 -0
  84. package/manager/FWBundleManager.ts.meta +10 -0
  85. package/manager/FWComponentManager.ts +24 -0
  86. package/manager/FWComponentManager.ts.meta +10 -0
  87. package/manager/FWEngineManager.ts +74 -0
  88. package/manager/FWEngineManager.ts.meta +10 -0
  89. package/manager/FWEventManager.ts +361 -0
  90. package/manager/FWEventManager.ts.meta +10 -0
  91. package/manager/FWHotUpdateManager.ts +344 -0
  92. package/manager/FWHotUpdateManager.ts.meta +10 -0
  93. package/manager/FWLanguageManager.ts +114 -0
  94. package/manager/FWLanguageManager.ts.meta +10 -0
  95. package/manager/FWLayerManager.ts +862 -0
  96. package/manager/FWLayerManager.ts.meta +10 -0
  97. package/manager/FWManager.ts +9 -0
  98. package/manager/FWManager.ts.meta +10 -0
  99. package/manager/FWObjectManager.ts +161 -0
  100. package/manager/FWObjectManager.ts.meta +10 -0
  101. package/manager/FWPerformanceManager.ts +198 -0
  102. package/manager/FWPerformanceManager.ts.meta +10 -0
  103. package/manager/FWPromiseManager.ts +504 -0
  104. package/manager/FWPromiseManager.ts.meta +10 -0
  105. package/manager/FWResManager.ts +299 -0
  106. package/manager/FWResManager.ts.meta +10 -0
  107. package/manager/FWSocketManager.ts +93 -0
  108. package/manager/FWSocketManager.ts.meta +10 -0
  109. package/manager/FWStateManager.ts +100 -0
  110. package/manager/FWStateManager.ts.meta +10 -0
  111. package/manager/FWTaskManager.ts +41 -0
  112. package/manager/FWTaskManager.ts.meta +10 -0
  113. package/manager/FWTimeManager.ts +442 -0
  114. package/manager/FWTimeManager.ts.meta +10 -0
  115. package/manager/FWUiManager.ts +217 -0
  116. package/manager/FWUiManager.ts.meta +10 -0
  117. package/manager.meta +13 -0
  118. package/package.json +11 -0
  119. package/package.json.meta +6 -0
  120. package/registry/FWRegistry.ts +62 -0
  121. package/registry/FWRegistry.ts.meta +10 -0
  122. package/registry.meta +13 -0
  123. package/render/FWRenderAssembler.ts +620 -0
  124. package/render/FWRenderAssembler.ts.meta +10 -0
  125. package/render.meta +13 -0
  126. package/scene/FWScene.ts +23 -0
  127. package/scene/FWScene.ts.meta +10 -0
  128. package/scene.meta +13 -0
  129. package/service/FWService.ts +11 -0
  130. package/service/FWService.ts.meta +10 -0
  131. package/service/http/FWHttp.ts +103 -0
  132. package/service/http/FWHttp.ts.meta +10 -0
  133. package/service/http.meta +13 -0
  134. package/service/socket/FWSocket.ts +367 -0
  135. package/service/socket/FWSocket.ts.meta +10 -0
  136. package/service/socket/FWSocketHandle.ts +16 -0
  137. package/service/socket/FWSocketHandle.ts.meta +10 -0
  138. package/service/socket/FWSocketSender.ts +14 -0
  139. package/service/socket/FWSocketSender.ts.meta +10 -0
  140. package/service/socket/mock/FWSocketMock.ts +202 -0
  141. package/service/socket/mock/FWSocketMock.ts.meta +10 -0
  142. package/service/socket/mock.meta +13 -0
  143. package/service/socket.meta +13 -0
  144. package/service.meta +13 -0
  145. package/state/FWState.ts +8 -0
  146. package/state/FWState.ts.meta +10 -0
  147. package/state.meta +13 -0
  148. package/types/Creator.d.ts +121 -0
  149. package/types/Creator.d.ts.meta +6 -0
  150. package/types/FW.d.ts +3016 -0
  151. package/types/FW.d.ts.meta +6 -0
  152. package/types/Global.d.ts +22 -0
  153. package/types/Global.d.ts.meta +10 -0
  154. package/types.meta +13 -0
  155. package/utils/FWLodash.ts +105 -0
  156. package/utils/FWLodash.ts.meta +10 -0
  157. package/utils/FWMask.ts +222 -0
  158. package/utils/FWMask.ts.meta +10 -0
  159. package/utils/FWObject.ts +27 -0
  160. package/utils/FWObject.ts.meta +10 -0
  161. package/utils/FWObjectPool.ts +177 -0
  162. package/utils/FWObjectPool.ts.meta +10 -0
  163. package/utils/FWQueue.ts +55 -0
  164. package/utils/FWQueue.ts.meta +10 -0
  165. package/utils/FWResLoader.ts +139 -0
  166. package/utils/FWResLoader.ts.meta +10 -0
  167. package/utils/FWTask.ts +227 -0
  168. package/utils/FWTask.ts.meta +10 -0
  169. package/utils/FWUtils.ts +15 -0
  170. package/utils/FWUtils.ts.meta +10 -0
  171. package/utils.meta +13 -0
package/render.meta ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "ver": "1.1.3",
3
+ "uuid": "d49ee3cc-a25e-4c14-8a04-5f22d03b8164",
4
+ "importer": "folder",
5
+ "isBundle": false,
6
+ "bundleName": "",
7
+ "priority": 1,
8
+ "compressionType": {},
9
+ "optimizeHotUpdate": {},
10
+ "inlineSpriteFrames": {},
11
+ "isRemoteBundle": {},
12
+ "subMetas": {}
13
+ }
@@ -0,0 +1,23 @@
1
+ const { ccclass, property } = cc._decorator;
2
+
3
+ @ccclass
4
+ export abstract class FWScene extends cc.Component {
5
+ readonly entry: FW.Entry = FW.Entry;
6
+
7
+ @FWSocketAutoProcessResume()
8
+ onLoad(): void {
9
+ this.entry.scene = this;
10
+ }
11
+
12
+ update(dt: number): void {
13
+ this.entry?.update?.(dt);
14
+ }
15
+
16
+ onDestroy(): void {
17
+ this.entry?.evtMgr.targetOff(this);
18
+ this.entry?.timeMgr.unSchedule(this);
19
+ this.entry?.uiMgr.unRegisterTarget(this);
20
+ this?.unscheduleAllCallbacks?.();
21
+ this?.node?.stopAllActions?.();
22
+ }
23
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "ver": "1.1.0",
3
+ "uuid": "9d78ed88-e34f-455b-9d0f-73978b74107a",
4
+ "importer": "typescript",
5
+ "isPlugin": false,
6
+ "loadPluginInWeb": true,
7
+ "loadPluginInNative": true,
8
+ "loadPluginInEditor": false,
9
+ "subMetas": {}
10
+ }
package/scene.meta ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "ver": "1.1.3",
3
+ "uuid": "f8d1e84d-5adb-468b-9dcf-c84901b90bce",
4
+ "importer": "folder",
5
+ "isBundle": false,
6
+ "bundleName": "",
7
+ "priority": 1,
8
+ "compressionType": {},
9
+ "optimizeHotUpdate": {},
10
+ "inlineSpriteFrames": {},
11
+ "isRemoteBundle": {},
12
+ "subMetas": {}
13
+ }
@@ -0,0 +1,11 @@
1
+ import { FrameworkBase } from '../FrameworkBase';
2
+
3
+ export abstract class FWService extends FrameworkBase {
4
+ public abstract initialize();
5
+ public abstract onDestroy(): void;
6
+
7
+ protected onRestart() {
8
+ this.onDestroy();
9
+ this.initialize();
10
+ }
11
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "ver": "1.1.0",
3
+ "uuid": "02e35f42-98e4-45c5-8139-e90f226d3782",
4
+ "importer": "typescript",
5
+ "isPlugin": false,
6
+ "loadPluginInWeb": true,
7
+ "loadPluginInNative": true,
8
+ "loadPluginInEditor": false,
9
+ "subMetas": {}
10
+ }
@@ -0,0 +1,103 @@
1
+ import { FWSystemConfig } from "../../config/FWSystemConfig";
2
+
3
+ export class FWHttp extends FW.Service {
4
+ public initialize() {}
5
+ public onDestroy(): void {}
6
+ protected onRestart(): void {}
7
+
8
+ protected async getMessage(
9
+ url: string,
10
+ params?: string,
11
+ cb?: (response: string) => void,
12
+ tag?: string,
13
+ ): Promise<string> {
14
+ return this.process(
15
+ url,
16
+ params,
17
+ cb,
18
+ tag,
19
+ FW.SystemDefine.FWHttpRequestType.GET,
20
+ );
21
+ }
22
+
23
+ protected async postMessage(
24
+ url: string,
25
+ params?: string,
26
+ cb?: (response: string) => void,
27
+ tag?: string,
28
+ ): Promise<string> {
29
+ return this.process(
30
+ url,
31
+ params,
32
+ cb,
33
+ tag,
34
+ FW.SystemDefine.FWHttpRequestType.POST,
35
+ );
36
+ }
37
+
38
+ private async process(
39
+ url: string,
40
+ params: string,
41
+ cb: (response: any) => void,
42
+ tag: string,
43
+ type: FW.SystemDefine.FWHttpRequestType,
44
+ ): Promise<string> {
45
+ let xhr: XMLHttpRequest;
46
+
47
+ const promiseProxy: FW.PromiseProxy<string> =
48
+ FW.Entry.promiseMgr.execute<string>(
49
+ (resolve, reject, signal, reason) => {
50
+ xhr = new XMLHttpRequest();
51
+
52
+ xhr.onreadystatechange = () => {
53
+ if (xhr.readyState !== 4) return;
54
+
55
+ if (xhr.status >= 200 && xhr.status < 300) {
56
+ cb?.(xhr.response);
57
+ resolve(xhr.response);
58
+ return;
59
+ }
60
+
61
+ const errMsg = `http request failed, status: ${xhr.status}, url: ${url}`;
62
+ FW.Log.warn(errMsg);
63
+ reject(errMsg);
64
+ };
65
+
66
+ xhr.onerror = (err: any) => {
67
+ FW.Log.error("http request error:", err);
68
+ this.onError?.(err);
69
+ reject(`http request error:${err}`);
70
+ };
71
+
72
+ xhr.ontimeout = () => {
73
+ this.onTimeout?.();
74
+ reject(`http request timeout!`);
75
+ };
76
+
77
+ xhr.open(type as string, url, true);
78
+ xhr.send(params);
79
+ },
80
+ {
81
+ ...FWSystemConfig.PromiseConfig.http,
82
+ retryCondition(error, retryCount) {
83
+ return (
84
+ !!error &&
85
+ retryCount < (FWSystemConfig.PromiseConfig.http.retryCount || 0)
86
+ );
87
+ },
88
+ },
89
+ );
90
+
91
+ promiseProxy.addAbortEventListener(() => {
92
+ xhr.abort();
93
+ });
94
+
95
+ return await this.invoke(
96
+ promiseProxy.promise,
97
+ tag ? `http request ->${tag}` : "",
98
+ );
99
+ }
100
+
101
+ onError?(err: any);
102
+ onTimeout?();
103
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "ver": "1.1.0",
3
+ "uuid": "f0ad42b9-3784-4643-9175-bffd9d96570a",
4
+ "importer": "typescript",
5
+ "isPlugin": false,
6
+ "loadPluginInWeb": true,
7
+ "loadPluginInNative": true,
8
+ "loadPluginInEditor": false,
9
+ "subMetas": {}
10
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "ver": "1.1.3",
3
+ "uuid": "8af24859-b6c3-4ead-8cf8-386570d860a8",
4
+ "importer": "folder",
5
+ "isBundle": false,
6
+ "bundleName": "",
7
+ "priority": 1,
8
+ "compressionType": {},
9
+ "optimizeHotUpdate": {},
10
+ "inlineSpriteFrames": {},
11
+ "isRemoteBundle": {},
12
+ "subMetas": {}
13
+ }
@@ -0,0 +1,367 @@
1
+ import { FWSystemConfig } from '../../config/FWSystemConfig';
2
+
3
+ /**
4
+ * TODO 犹豫socket没有改版暂时已老的cmd字符串映射方式做,后期优化
5
+ */
6
+ export default class FWSocket extends FW.Service implements FW.Socket {
7
+ /** 发送心跳时间抽 */
8
+ private sendHeartTimestamp: number;
9
+ /** 接收心跳时间戳 */
10
+ private receiveTimeStamp: number;
11
+ /** socket */
12
+ socket: WebSocket = null;
13
+ /** 消息发送者 */
14
+ protected socketSender: FW.Sender;
15
+ /** 消息处理者 */
16
+ protected socketHandle: FW.Handle;
17
+ /** 重连次数 */
18
+ protected reconnectTimes: number;
19
+ /** 心跳间隔时间 */
20
+ protected heartInternal: number;
21
+ /** 心跳超时时间 */
22
+ protected heartTimeout: number;
23
+ /** 心跳弱网时间 */
24
+ protected heartWeakTime: number;
25
+ /** 最大重连次数 */
26
+ protected maxReconnectTimes: number;
27
+ /** 重连间隔 */
28
+ protected reconnectInternal: number;
29
+ /** 地址 */
30
+ protected address: string;
31
+ /** 标签 */
32
+ protected tag: string;
33
+ /** 是否暂停消息处理 */
34
+ protected isPausedMessageHandle: boolean = false;
35
+ /** 代理 */
36
+ private promiseProxy: FW.SocketPromiseProxy;
37
+ /** 协议符号 */
38
+ private protocolSymbol: FW.Symbol;
39
+ /** 协议轮询时间 */
40
+ private protocolPollingTime: number;
41
+ /** 协议注册表 */
42
+ private protocolRegistry: Map<string, Set<Symbol>>;
43
+ /** 协议符号容器 */
44
+ private protocolContainer: Map<Symbol, FW.ProtocolPolling>;
45
+ /** 证书 */
46
+ private certificate: cc.Asset;
47
+ /** 是否强制关闭 */
48
+ private forceClose: boolean;
49
+
50
+ /**
51
+ * 消息事件列表
52
+ * TODO 目前没有消息协议号,暂时无用
53
+ */
54
+ protected messageEvents: { [key: string]: (msg: any) => void };
55
+
56
+ public initialize(): void {
57
+ this.messageEvents = cc.js.createMap();
58
+ this.protocolContainer = new Map<Symbol, FW.ProtocolPolling>();
59
+ this.protocolRegistry = new Map<string, Set<Symbol>>();
60
+ this.reconnectTimes = 0;
61
+ }
62
+
63
+ createSocket(
64
+ address: string,
65
+ tag: string,
66
+ socketSender: FW.Sender,
67
+ socketHandle: FW.Handle,
68
+ config: FW.SocketConfig,
69
+ ): FW.SocketPromiseProxy {
70
+ try {
71
+ FW.Entry.timeMgr.unSchedule(this);
72
+
73
+ this.tag = tag;
74
+ this.address = address;
75
+ this.forceClose = false;
76
+ this.clear();
77
+
78
+ this.messageEvents = {};
79
+ this.sendHeartTimestamp = 0;
80
+ this.receiveTimeStamp = new Date().getTime();
81
+
82
+ if (cc.sys.isBrowser) {
83
+ this.socket = new WebSocket(address);
84
+ } else {
85
+ if (config.certificate) {
86
+ this.certificate = config.certificate;
87
+
88
+ let url = config.certificate.nativeUrl;
89
+ if (cc.assetManager.cacheManager) {
90
+ url =
91
+ cc.assetManager.cacheManager.getCache(url) ||
92
+ cc.assetManager.cacheManager.getTemp(url) ||
93
+ url;
94
+ }
95
+
96
+ //@ts-ignore
97
+ this.socket = new WebSocket(address, [], url);
98
+ } else {
99
+ this.socket = new WebSocket(address);
100
+ }
101
+ }
102
+ this.socket.onopen = this.onSocketOpen.bind(this);
103
+ this.socket.onclose = this.onSocketClose.bind(this);
104
+ this.socket.onerror = this.onSocketError.bind(this);
105
+ this.socket.onmessage = this.onSocketMessage.bind(this);
106
+
107
+ this.socketSender = socketSender;
108
+ this.socketHandle = socketHandle;
109
+
110
+ const defaultConfig = FWSystemConfig.SocketDefaultConfig;
111
+ this.heartInternal = config?.heartInternal || defaultConfig.heartInternal;
112
+ this.heartTimeout = config?.heartTimeout || defaultConfig.heartTimeout;
113
+ this.heartWeakTime = config?.heartWeakTime || defaultConfig.heartWeakTime;
114
+ this.maxReconnectTimes = config?.maxReconnectTimes || defaultConfig.maxReconnectTimes;
115
+ this.reconnectInternal = config?.reconnectInternal || defaultConfig.reconnectInternal;
116
+ this.protocolSymbol = config?.protocolSymbol || defaultConfig.protocolSymbol;
117
+ this.protocolPollingTime = config?.protocolPollingTime || defaultConfig.protocolPollingTime;
118
+
119
+ this.promiseProxy = {
120
+ promise: undefined,
121
+ resolve: undefined,
122
+ socket: this,
123
+ };
124
+
125
+ this.promiseProxy.promise = new Promise<FW.Socket>((resolve) => {
126
+ this.promiseProxy.resolve = resolve;
127
+ });
128
+
129
+ return this.promiseProxy;
130
+ } catch (e) {
131
+ FW.Log.error('创建socket失败:', e);
132
+ }
133
+ }
134
+ /**
135
+ * 注册消息事件
136
+ * @param msgId
137
+ * @param cb
138
+ */
139
+ registerMessage(msgId: string, cb: (msg: any) => void) {
140
+ this.messageEvents[msgId] = cb;
141
+ }
142
+
143
+ public onDestroy(): void {
144
+ this.messageEvents = cc.js.createMap();
145
+ this.close();
146
+ }
147
+
148
+ public getTag() {
149
+ return this.tag;
150
+ }
151
+
152
+ /**
153
+ * 获取地址
154
+ * @returns
155
+ */
156
+ public getAddress() {
157
+ return this.address;
158
+ }
159
+
160
+ /**
161
+ * 获取socket状态
162
+ * @returns
163
+ */
164
+ public getReadyState() {
165
+ return this.socket?.readyState;
166
+ }
167
+ /**
168
+ * 关闭连接
169
+ */
170
+ public close() {
171
+ this.clear();
172
+ this.forceClose = true;
173
+ this.onSocketClose();
174
+ this.socket?.close();
175
+ this.socket = null;
176
+ FW.Entry.timeMgr.unSchedule(this);
177
+ }
178
+
179
+ public pauseMessageHandle() {
180
+ this.isPausedMessageHandle = true;
181
+ }
182
+
183
+ public resumeMessageHandle() {
184
+ this.isPausedMessageHandle = false;
185
+ }
186
+
187
+ public get isReconnect() {
188
+ return this.getReadyState() === WebSocket.CONNECTING;
189
+ }
190
+
191
+ public clear() {
192
+ this.protocolContainer.clear();
193
+ this.protocolRegistry.clear();
194
+ }
195
+
196
+ reconnect(force?: boolean) {
197
+ if (force) this.reconnectTimes = 0;
198
+
199
+ FW.Log.debug(
200
+ `socket reconnect : reconnectTimes->${this.reconnectTimes},maxReconnectTimes->${this.maxReconnectTimes}`,
201
+ );
202
+ if (++this.reconnectTimes >= this.maxReconnectTimes) {
203
+ this.socketHandle?.onTimeout?.();
204
+ return;
205
+ }
206
+ this.clear();
207
+
208
+ this.socket?.close();
209
+ this.socket = null;
210
+
211
+ this.createSocket(this.address, this.tag, this.socketSender, this.socketHandle, {
212
+ heartInternal: this.heartInternal,
213
+ heartTimeout: this.heartTimeout,
214
+ heartWeakTime: this.heartWeakTime,
215
+ maxReconnectTimes: this.maxReconnectTimes,
216
+ reconnectInternal: this.reconnectInternal,
217
+ protocolSymbol: this.protocolSymbol,
218
+ protocolPollingTime: this.protocolPollingTime,
219
+ certificate: this.certificate,
220
+ });
221
+ }
222
+
223
+ async send(msg: string) {
224
+ if (this.getReadyState() != WebSocket.OPEN) return;
225
+ if (this.isPausedMessageHandle) return;
226
+ if (!(await this.socketSender.onHeart?.(msg))) {
227
+ const protocolKey = this.socketSender.getProtocolKey?.(msg);
228
+ if (protocolKey) {
229
+ const symbol = Symbol(protocolKey);
230
+ const registry = this.protocolRegistry.get(protocolKey) || new Set<Symbol>();
231
+ const protocolPolling: FW.ProtocolPolling = {
232
+ msg: msg,
233
+ schedule: FW.Entry.timeMgr.schedule(
234
+ () => {
235
+ this.sendMessage(msg);
236
+ },
237
+ this.protocolPollingTime / 1000,
238
+ cc.macro.REPEAT_FOREVER,
239
+ this,
240
+ ),
241
+ };
242
+ this.protocolRegistry.set(protocolKey, registry.add(symbol));
243
+ this.protocolContainer.set(symbol, protocolPolling);
244
+ }
245
+ }
246
+ this.sendMessage(msg);
247
+ }
248
+
249
+ /** 连接打开 */
250
+ private onSocketOpen() {
251
+ FW.Log.debug('on open!');
252
+ FW.Entry.timeMgr.unSchedule(this);
253
+ this.reconnectTimes = 0;
254
+ this.sendHeartTimestamp = 0;
255
+ this.receiveTimeStamp = new Date().getTime();
256
+ this.socketHandle?.onOpen?.();
257
+ this.heartHandle();
258
+ this.promiseProxy?.resolve(this);
259
+ }
260
+
261
+ private heartHandle() {
262
+ FW.Entry?.timeMgr?.schedule(
263
+ () => {
264
+ const interval = this.sendHeartTimestamp - this.receiveTimeStamp;
265
+ if (interval >= this.heartWeakTime) {
266
+ this.socketHandle.onWeakNetWork?.();
267
+ if (interval >= this.heartTimeout) {
268
+ this.reconnect();
269
+ return;
270
+ }
271
+ }
272
+ this.sendHeart();
273
+ },
274
+ this.heartInternal / 1000,
275
+ cc.macro.REPEAT_FOREVER,
276
+ this,
277
+ );
278
+ }
279
+
280
+ private sendHeart() {
281
+ this.sendHeartTimestamp = new Date().getTime();
282
+ this.socketSender?.sendHeart?.();
283
+ }
284
+
285
+ private sendMessage(msg: any) {
286
+ try {
287
+ this.socket?.send(this.socketSender.onBeforeSendingMessage?.(msg) || msg);
288
+ } catch (e) {
289
+ this.socket?.send(msg);
290
+ }
291
+ }
292
+
293
+ /** socket关闭 */
294
+ private onSocketClose() {
295
+ FW.Log.debug('on close!');
296
+ this.socketHandle?.onClose?.();
297
+
298
+ if (this.socket) {
299
+ this.socket.onclose = null;
300
+ this.socket.onerror = null;
301
+ this.socket.onmessage = null;
302
+ this.socket.onopen = null;
303
+ }
304
+
305
+ FW.Entry.timeMgr.scheduleOnce(
306
+ () => {
307
+ if (
308
+ !this.forceClose &&
309
+ (this.getReadyState() == WebSocket.CLOSING || this.getReadyState() == WebSocket.CLOSED)
310
+ ) {
311
+ FW.Log.debug('on close!');
312
+ this.reconnect();
313
+ }
314
+ },
315
+ this.reconnectInternal / 1000,
316
+ this,
317
+ );
318
+ }
319
+
320
+ /** 消息处理 */
321
+ private async onSocketMessage(originalMsg: any) {
322
+ const msg = await this.socketHandle.onBeforeReceivingMessage(originalMsg);
323
+
324
+ if (!msg || msg?.error) {
325
+ this.onSocketError(msg);
326
+ return;
327
+ }
328
+
329
+ if (await this.socketHandle?.onHeart?.(msg)) {
330
+ this.receiveTimeStamp = new Date().getTime();
331
+ return;
332
+ }
333
+
334
+ this.remoteProtocol(msg);
335
+
336
+ if (this.isPausedMessageHandle) return;
337
+
338
+ this.socketHandle?.onMessage?.(msg);
339
+ }
340
+ /** 错误处理 */
341
+ private async onSocketError(msg: any) {
342
+ await this.remoteProtocol(msg);
343
+ this.socketHandle?.onError?.(msg);
344
+ }
345
+
346
+ private async remoteProtocol(msg: any) {
347
+ const protocolKey = await this.socketHandle.getProtocolKey?.(msg);
348
+ if (protocolKey && this.protocolRegistry.has(protocolKey)) {
349
+ const symbolSet = this.protocolRegistry.get(protocolKey);
350
+
351
+ if (symbolSet && symbolSet.size > 0) {
352
+ const [symbol] = symbolSet;
353
+ const protocolPolling = this.protocolContainer.get(symbol);
354
+
355
+ symbolSet.delete(symbol);
356
+
357
+ protocolPolling.schedule.unSchedule();
358
+
359
+ this.protocolContainer.delete(symbol);
360
+
361
+ if (symbolSet.size === 0) {
362
+ this.protocolRegistry.delete(protocolKey);
363
+ }
364
+ }
365
+ }
366
+ }
367
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "ver": "1.1.0",
3
+ "uuid": "6bff4169-033b-4745-842d-fdb124b9ac6c",
4
+ "importer": "typescript",
5
+ "isPlugin": false,
6
+ "loadPluginInWeb": true,
7
+ "loadPluginInNative": true,
8
+ "loadPluginInEditor": false,
9
+ "subMetas": {}
10
+ }
@@ -0,0 +1,16 @@
1
+ import { injectable } from 'inversify';
2
+ import { FrameworkBase } from '../../FrameworkBase';
3
+ @injectable()
4
+ export abstract class FWSocketHandle extends FrameworkBase {
5
+ initialize?();
6
+ onDestroy?();
7
+ onWeakNetWork?(): void;
8
+ abstract onMessage(msg: any): void;
9
+ abstract onHeart?(msg: any): Promise<boolean>;
10
+ abstract onClose?(): void;
11
+ abstract onOpen?(): void;
12
+ abstract onError?(msg: any): void;
13
+ abstract onTimeout?(): void;
14
+ abstract getProtocolKey?(msg: any): string;
15
+ abstract onBeforeReceivingMessage?(msg: any): Promise<FW.SocketMessage>;
16
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "ver": "1.1.0",
3
+ "uuid": "2e34eee4-ca3e-4ffd-9ff5-3fab23ea4f76",
4
+ "importer": "typescript",
5
+ "isPlugin": false,
6
+ "loadPluginInWeb": true,
7
+ "loadPluginInNative": true,
8
+ "loadPluginInEditor": false,
9
+ "subMetas": {}
10
+ }
@@ -0,0 +1,14 @@
1
+ import { injectable } from 'inversify';
2
+ import { FrameworkBase } from '../../FrameworkBase';
3
+ @injectable()
4
+ export abstract class FWSocketSender extends FrameworkBase {
5
+ initialize?();
6
+ onDestroy?();
7
+ sendHeart?();
8
+ onHeart?(msg: any): Promise<boolean>;
9
+ abstract onBeforeSendingMessage?(msg: any): Promise<FW.SocketMessage>;
10
+ abstract getProtocolKey?(msg: any): string;
11
+ send(msg: any) {
12
+ FW.Entry.socketMgr.getSocket()?.send(msg);
13
+ }
14
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "ver": "1.1.0",
3
+ "uuid": "5ef12e31-ee4f-443b-9de1-9075ecdde103",
4
+ "importer": "typescript",
5
+ "isPlugin": false,
6
+ "loadPluginInWeb": true,
7
+ "loadPluginInNative": true,
8
+ "loadPluginInEditor": false,
9
+ "subMetas": {}
10
+ }