|
@@ -72,10 +72,7 @@ class Client {
|
72
|
72
|
*/
|
73
|
73
|
public async ping(param: object): Promise<string> {
|
74
|
74
|
return new Promise(
|
75
|
|
- (
|
76
|
|
- resolve: (data: string) => void,
|
77
|
|
- reject: (err: WebsocketError) => void,
|
78
|
|
- ): void => {
|
|
75
|
+ (resolve: (data: string) => void, reject: (err: WebsocketError) => void): void => {
|
79
|
76
|
if (this.socket.readyState !== this.socket.OPEN) {
|
80
|
77
|
if (this._enableLogger) {
|
81
|
78
|
console.log('[ping]: connection refuse');
|
|
@@ -96,29 +93,16 @@ class Client {
|
96
|
93
|
} else {
|
97
|
94
|
resolve(data);
|
98
|
95
|
}
|
99
|
|
- },
|
|
96
|
+ }
|
100
|
97
|
);
|
101
|
98
|
|
102
|
99
|
const p = new Packet();
|
103
|
|
- this.send(
|
104
|
|
- p.pack(
|
105
|
|
- heartbeatOperator,
|
106
|
|
- 0,
|
107
|
|
- this.requestHeader,
|
108
|
|
- JSON.stringify(param),
|
109
|
|
- ),
|
110
|
|
- );
|
|
100
|
+ this.send(p.pack(heartbeatOperator, 0, this.requestHeader, JSON.stringify(param)));
|
111
|
101
|
|
112
|
102
|
if (this._enableLogger) {
|
113
|
|
- console.info(
|
114
|
|
- '[send data packet]',
|
115
|
|
- heartbeatOperator,
|
116
|
|
- 0,
|
117
|
|
- this.requestHeader,
|
118
|
|
- param,
|
119
|
|
- );
|
|
103
|
+ console.info('[send data packet]', heartbeatOperator, 0, this.requestHeader, param);
|
120
|
104
|
}
|
121
|
|
- },
|
|
105
|
+ }
|
122
|
106
|
);
|
123
|
107
|
}
|
124
|
108
|
|
|
@@ -138,10 +122,7 @@ class Client {
|
138
|
122
|
* @param operator 消息监听地址
|
139
|
123
|
* @param listener 定义如何处理从服务端返回的消息
|
140
|
124
|
*/
|
141
|
|
- public addMessageListener(
|
142
|
|
- operator: string,
|
143
|
|
- listener: (data: string) => void,
|
144
|
|
- ): void {
|
|
125
|
+ public addMessageListener(operator: string, listener: (data: string) => void): void {
|
145
|
126
|
this.listeners.set(Utils.crc32(operator), listener);
|
146
|
127
|
}
|
147
|
128
|
|
|
@@ -280,9 +261,7 @@ class Client {
|
280
|
261
|
|
281
|
262
|
this.responseHeader = packet.header;
|
282
|
263
|
|
283
|
|
- (this.listeners.get(operator) as (data: string) => void)(
|
284
|
|
- JSON.parse(packet.body),
|
285
|
|
- );
|
|
264
|
+ (this.listeners.get(operator) as (data: string) => void)(JSON.parse(packet.body));
|
286
|
265
|
}
|
287
|
266
|
|
288
|
267
|
if (this._enableLogger) {
|
|
@@ -328,9 +307,7 @@ class Client {
|
328
|
307
|
private send(data: ArrayBuffer): void {
|
329
|
308
|
if (this.socket.readyState !== this.socket.OPEN) {
|
330
|
309
|
if (this._enableLogger) {
|
331
|
|
- console.error(
|
332
|
|
- '[send] WebSocket is already in CLOSING or CLOSED state.',
|
333
|
|
- );
|
|
310
|
+ console.error('[send] WebSocket is already in CLOSING or CLOSED state.');
|
334
|
311
|
}
|
335
|
312
|
|
336
|
313
|
return;
|
|
@@ -351,18 +328,13 @@ class Client {
|
351
|
328
|
*/
|
352
|
329
|
private asyncSend(operator: string, param: object): Promise<string> {
|
353
|
330
|
return new Promise(
|
354
|
|
- (
|
355
|
|
- resolve: (data: string) => void,
|
356
|
|
- reject: (err: WebsocketError) => void,
|
357
|
|
- ): void => {
|
|
331
|
+ (resolve: (data: string) => void, reject: (err: WebsocketError) => void): void => {
|
358
|
332
|
if (this.socket.readyState !== this.socket.OPEN) {
|
359
|
333
|
if (this._enableLogger) {
|
360
|
334
|
console.log('[ping]: connection refuse');
|
361
|
335
|
}
|
362
|
336
|
|
363
|
|
- reject(
|
364
|
|
- new WebsocketError(clientError, 'asyncSend: connection refuse'),
|
365
|
|
- );
|
|
337
|
+ reject(new WebsocketError(clientError, 'asyncSend: connection refuse'));
|
366
|
338
|
}
|
367
|
339
|
|
368
|
340
|
const sequence = new Date().getTime();
|
|
@@ -379,29 +351,18 @@ class Client {
|
379
|
351
|
}
|
380
|
352
|
|
381
|
353
|
delete this.listeners[listener];
|
382
|
|
- },
|
|
354
|
+ }
|
383
|
355
|
);
|
384
|
356
|
|
385
|
357
|
const p = new Packet();
|
386
|
358
|
this.send(
|
387
|
|
- p.pack(
|
388
|
|
- Utils.crc32(operator),
|
389
|
|
- sequence,
|
390
|
|
- this.requestHeader,
|
391
|
|
- JSON.stringify(param),
|
392
|
|
- ),
|
|
359
|
+ p.pack(Utils.crc32(operator), sequence, this.requestHeader, JSON.stringify(param))
|
393
|
360
|
);
|
394
|
361
|
|
395
|
362
|
if (this._enableLogger) {
|
396
|
|
- console.info(
|
397
|
|
- '[send data packet]',
|
398
|
|
- operator,
|
399
|
|
- sequence,
|
400
|
|
- this.requestHeader,
|
401
|
|
- param,
|
402
|
|
- );
|
|
363
|
+ console.info('[send data packet]', operator, sequence, this.requestHeader, param);
|
403
|
364
|
}
|
404
|
|
- },
|
|
365
|
+ }
|
405
|
366
|
);
|
406
|
367
|
}
|
407
|
368
|
}
|