@ives_xxz/framework 2.0.13 → 2.0.14

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.
@@ -56,6 +56,7 @@ export function initializeFramework() {
56
56
  FW.Log = new (require('./log/FWLog').FWLog)();
57
57
  FW.Layer = require('./layer/FWLayer').FWLayer;
58
58
  FW.LayerController = require('./controller/FWLayerController').FWLayerController;
59
+ FW.SocketMock = new (require('./service/socket/mock/FWSocketMock').FWSocketMock)();
59
60
  FW.Framework.initialize();
60
61
  FW.Entry.initialize();
61
62
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ives_xxz/framework",
3
- "version": "2.0.13",
3
+ "version": "2.0.14",
4
4
  "description": "cocoscreator 2.x mvc framework",
5
5
  "main": "index.js",
6
6
  "keywords": ["123456"],
@@ -94,7 +94,6 @@ export default class FWSocket extends FW.Service implements FW.Socket {
94
94
  this.socket = new WebSocket(address);
95
95
  }
96
96
  }
97
-
98
97
  this.socket.onopen = this.onSocketOpen.bind(this);
99
98
  this.socket.onclose = this.onSocketClose.bind(this);
100
99
  this.socket.onerror = this.onSocketError.bind(this);
@@ -303,7 +302,7 @@ export default class FWSocket extends FW.Service implements FW.Socket {
303
302
  private async onSocketMessage(originalMsg: any) {
304
303
  const msg = await this.socketHandle.onBeforeReceivingMessage(originalMsg);
305
304
 
306
- if (msg.error) {
305
+ if (!msg || msg?.error) {
307
306
  this.onSocketError(msg);
308
307
  return;
309
308
  }
@@ -0,0 +1,203 @@
1
+ class MockWebSocket {
2
+ public url: string;
3
+ public readyState: number;
4
+ public binaryType: BinaryType = 'blob';
5
+
6
+ static readonly CONNECTING = 0;
7
+ static readonly OPEN = 1;
8
+ static readonly CLOSING = 2;
9
+ static readonly CLOSED = 3;
10
+
11
+ public onopen: ((event: Event) => void) | null = null;
12
+ public onmessage: ((event: MessageEvent) => void) | null = null;
13
+ public onerror: ((event: Event) => void) | null = null;
14
+ public onclose: ((event: CloseEvent) => void) | null = null;
15
+
16
+ private messageQueue: any[] = [];
17
+
18
+ constructor(url: string | URL, protocols?: string | string[]) {
19
+ this.url = url.toString();
20
+ this.readyState = MockWebSocket.CONNECTING;
21
+
22
+ FW.Entry.timeMgr.scheduleOnce(() => {
23
+ this.readyState = MockWebSocket.OPEN;
24
+
25
+ this.onopen?.(new Event('open'));
26
+
27
+ WebSocketMockServer.getInstance().addClient(this);
28
+
29
+ this.processMessageQueue();
30
+ });
31
+ }
32
+
33
+ send(data: string | ArrayBuffer | Blob | ArrayBufferView): void {
34
+ if (this.readyState !== MockWebSocket.OPEN) {
35
+ this.messageQueue.push(data);
36
+ return;
37
+ }
38
+
39
+ WebSocketMockServer.getInstance().handleMessage(this, data);
40
+ }
41
+
42
+ close(code?: number, reason?: string): void {
43
+ if (this.readyState === MockWebSocket.CLOSED) return;
44
+
45
+ this.readyState = MockWebSocket.CLOSED;
46
+ WebSocketMockServer.getInstance().removeClient(this);
47
+
48
+ const closeEvent = new CloseEvent('close', {
49
+ code: code || 1000,
50
+ reason: reason || 'Normal closure',
51
+ wasClean: true,
52
+ });
53
+
54
+ if (this.onclose) {
55
+ this.onclose(closeEvent);
56
+ }
57
+ }
58
+
59
+ private processMessageQueue(): void {
60
+ while (this.messageQueue.length > 0) {
61
+ const data = this.messageQueue.shift();
62
+ this.send(data);
63
+ }
64
+ }
65
+
66
+ mockServerSend(data: any): void {
67
+ if (this.readyState === MockWebSocket.OPEN) {
68
+ const messageData = typeof data === 'string' ? data : JSON.stringify(data);
69
+ const messageEvent = new MessageEvent('message', {
70
+ data: messageData,
71
+ });
72
+
73
+ this.onmessage?.(messageEvent);
74
+ }
75
+ }
76
+
77
+ mockServerError(error: any): void {
78
+ const errorEvent = new Event('error');
79
+
80
+ this.onerror?.(errorEvent);
81
+ }
82
+ }
83
+
84
+ class WebSocketMockServer {
85
+ private clients: Set<MockWebSocket> = new Set();
86
+ private broadcastIntervals: any[] = [];
87
+ private static instance: WebSocketMockServer;
88
+
89
+ public mockResponses = {};
90
+
91
+ public static getInstance(): WebSocketMockServer {
92
+ if (!WebSocketMockServer.instance) {
93
+ WebSocketMockServer.instance = new WebSocketMockServer();
94
+ }
95
+ return WebSocketMockServer.instance;
96
+ }
97
+
98
+ private constructor() {
99
+ FW.Log.system('🎮 WebSocket 模拟服务器已启动');
100
+ }
101
+
102
+ addClient(client: MockWebSocket): void {
103
+ this.clients.add(client);
104
+
105
+ FW.Log.system(`客户端连接,当前连接数: ${this.clients.size}`);
106
+
107
+ this.sendToClient(client, {
108
+ code: 0,
109
+ message: '连接到模拟游戏服务器成功',
110
+ serverTime: FW.Entry.timeMgr.getTime(),
111
+ });
112
+ }
113
+
114
+ removeClient(client: MockWebSocket): void {
115
+ this.clients.delete(client);
116
+ FW.Log.system(`客户端断开,当前连接数: ${this.clients.size}`);
117
+ }
118
+
119
+ handleClient(client: MockWebSocket, data: any): void {
120
+ try {
121
+ const message = typeof data === 'string' ? JSON.parse(data) : data;
122
+
123
+ const response = this.generateMockResponse(message);
124
+ this.sendToClient(client, response);
125
+ } catch (error) {
126
+ this.sendToClient(client, {
127
+ type: 'error',
128
+ message: '消息格式错误',
129
+ timestamp: new Date().toISOString(),
130
+ });
131
+ }
132
+ }
133
+
134
+ handleMessage(client: MockWebSocket, data: string | ArrayBuffer | Blob | ArrayBufferView): void {
135
+ if (typeof data === 'string') {
136
+ this.handleClient(client, data);
137
+ } else {
138
+ console.warn('二进制消息暂不支持');
139
+ }
140
+ }
141
+
142
+ private generateMockResponse(message: any): any {
143
+ return (
144
+ this.mockResponses[message.type as keyof typeof this.mockResponses] || {
145
+ type: 'unknown',
146
+ success: false,
147
+ message: { error: 'unknown message' },
148
+ }
149
+ );
150
+ }
151
+
152
+ private sendToClient(client: MockWebSocket, data: any): void {
153
+ client.mockServerSend(data);
154
+ }
155
+
156
+ public broadcast(data: any): void {
157
+ const message = typeof data === 'string' ? data : JSON.stringify(data);
158
+ this.clients.forEach((client) => {
159
+ this.sendToClient(client, message);
160
+ });
161
+ }
162
+
163
+ public stop(): void {
164
+ this.broadcastIntervals.forEach((interval) => clearInterval(interval));
165
+ this.broadcastIntervals = [];
166
+ this.clients.clear();
167
+ FW.Log.system('🛑 WebSocket 模拟服务器已停止');
168
+ }
169
+ }
170
+
171
+ export class FWSocketMock {
172
+ private isInitialized = false;
173
+
174
+ start(mockResponses: {}): void {
175
+ if (this.isInitialized) return;
176
+
177
+ WebSocketMockServer.getInstance().mockResponses = mockResponses;
178
+
179
+ const OriginalWebSocket = window.WebSocket;
180
+
181
+ (window as any).WebSocket = MockWebSocket;
182
+ (window as any).OriginalWebSocket = OriginalWebSocket;
183
+
184
+ this.isInitialized = true;
185
+ FW.Log.system('🎮 WebSocket 模拟已启动 - 所有 WebSocket 连接将被模拟');
186
+ }
187
+
188
+ stop(): void {
189
+ if (!this.isInitialized) return;
190
+
191
+ if ((window as any).OriginalWebSocket) {
192
+ window.WebSocket = (window as any).OriginalWebSocket;
193
+ }
194
+
195
+ WebSocketMockServer.getInstance().stop();
196
+ this.isInitialized = false;
197
+ FW.Log.system('🛑 WebSocket 模拟已停止 - 恢复原生 WebSocket');
198
+ }
199
+
200
+ getServer(): WebSocketMockServer {
201
+ return WebSocketMockServer.getInstance();
202
+ }
203
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "ver": "1.1.0",
3
+ "uuid": "a8d930bd-0449-4481-8ebd-e0664d7cd09f",
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": "755d7ddd-de7b-4f43-9a7a-1fbd31b2a0b9",
4
+ "importer": "folder",
5
+ "isBundle": false,
6
+ "bundleName": "",
7
+ "priority": 1,
8
+ "compressionType": {},
9
+ "optimizeHotUpdate": {},
10
+ "inlineSpriteFrames": {},
11
+ "isRemoteBundle": {},
12
+ "subMetas": {}
13
+ }
package/types/FW.d.ts CHANGED
@@ -2339,3 +2339,11 @@ declare namespace FW {
2339
2339
  onTimeout?();
2340
2340
  }
2341
2341
  }
2342
+
2343
+ declare namespace FW {
2344
+ export class SocketMock {
2345
+ static createMockData<T>(msgId: string, data: T);
2346
+ public static start(mockResponses: {}): void;
2347
+ public static stop(): void;
2348
+ }
2349
+ }