|
@@ -5,6 +5,7 @@ import {
|
5
|
5
|
WebSocketResp,
|
6
|
6
|
ReadyStateCallback,
|
7
|
7
|
} from './types/index';
|
|
8
|
+import EventEmitter from 'eventemitter3';
|
8
|
9
|
|
9
|
10
|
const clientError = 400;
|
10
|
11
|
|
|
@@ -15,7 +16,7 @@ class Client {
|
15
|
16
|
private _maxPayload: number;
|
16
|
17
|
private _enableLogger: boolean;
|
17
|
18
|
private static instance: Client;
|
18
|
|
- private listeners: Map<number, (data: WebSocketResp) => void>;
|
|
19
|
+ private listeners: EventEmitter;
|
19
|
20
|
private requestHeader: string;
|
20
|
21
|
private responseHeader: string;
|
21
|
22
|
private url: string;
|
|
@@ -30,7 +31,7 @@ class Client {
|
30
|
31
|
* @param readyStateCallback 链接状态回调,可以处理onOpen、onClose、onError
|
31
|
32
|
*/
|
32
|
33
|
private constructor(url: string, readyStateCallback: ReadyStateCallback) {
|
33
|
|
- this.listeners = new Map<number, (data: WebSocketResp) => void>();
|
|
34
|
+ this.listeners = new EventEmitter();
|
34
|
35
|
this.requestHeader = '';
|
35
|
36
|
this.requestHeader = '';
|
36
|
37
|
this._maxPayload = 1024 * 1024;
|
|
@@ -103,8 +104,8 @@ class Client {
|
103
|
104
|
|
104
|
105
|
const heartbeatOperator = 0;
|
105
|
106
|
|
106
|
|
- this.listeners.set(
|
107
|
|
- heartbeatOperator,
|
|
107
|
+ this.listeners.addListener(
|
|
108
|
+ heartbeatOperator.toString(),
|
108
|
109
|
(data: WebSocketResp): void => {
|
109
|
110
|
const code = this.getResponseProperty('code');
|
110
|
111
|
if (code !== '') {
|
|
@@ -162,7 +163,7 @@ class Client {
|
162
|
163
|
operator: string,
|
163
|
164
|
listener: (data: WebSocketResp) => void,
|
164
|
165
|
): void {
|
165
|
|
- this.listeners.set(Utils.crc32(operator), listener);
|
|
166
|
+ this.listeners.addListener(Utils.crc32(operator).toString(), listener);
|
166
|
167
|
}
|
167
|
168
|
|
168
|
169
|
/**
|
|
@@ -170,7 +171,7 @@ class Client {
|
170
|
171
|
* @param operator 消息监听地址
|
171
|
172
|
*/
|
172
|
173
|
public removeMessageListener(operator: string): void {
|
173
|
|
- delete this.listeners[Utils.crc32(operator)];
|
|
174
|
+ this.listeners.removeListener(Utils.crc32(operator).toString());
|
174
|
175
|
}
|
175
|
176
|
|
176
|
177
|
/**
|
|
@@ -298,17 +299,17 @@ class Client {
|
298
|
299
|
}
|
299
|
300
|
|
300
|
301
|
let operator = Number(packet.operator) + Number(packet.sequence);
|
301
|
|
- if (this.listeners.has(operator)) {
|
|
302
|
+ let listener = operator.toString();
|
|
303
|
+
|
|
304
|
+ this.listeners.listeners(listener).forEach((param): void => {
|
302
|
305
|
if (packet.body === '') {
|
303
|
306
|
packet.body = '{}';
|
304
|
307
|
}
|
305
|
308
|
|
306
|
309
|
this.responseHeader = packet.header;
|
307
|
310
|
|
308
|
|
- (this.listeners.get(operator) as (data: WebSocketResp) => void)(
|
309
|
|
- JSON.parse(packet.body),
|
310
|
|
- );
|
311
|
|
- }
|
|
311
|
+ (param as (data: WebSocketResp) => void)(JSON.parse(packet.body));
|
|
312
|
+ });
|
312
|
313
|
|
313
|
314
|
if (this._enableLogger) {
|
314
|
315
|
if (operator !== 0 && packet.body !== 'null') {
|
|
@@ -392,8 +393,8 @@ class Client {
|
392
|
393
|
|
393
|
394
|
const sequence = new Date().getTime();
|
394
|
395
|
const listener = Utils.crc32(operator) + sequence;
|
395
|
|
- this.listeners.set(
|
396
|
|
- listener,
|
|
396
|
+ this.listeners.addListener(
|
|
397
|
+ listener.toString(),
|
397
|
398
|
(data: WebSocketResp): void => {
|
398
|
399
|
const code = this.getResponseProperty('code');
|
399
|
400
|
if (code !== '') {
|