|
@@ -5,13 +5,17 @@ import { WebsocketError } from './error';
|
5
|
5
|
|
6
|
6
|
const clientError = 400;
|
7
|
7
|
|
|
8
|
+export interface WebSocketResp {
|
|
9
|
+ value: object | string;
|
|
10
|
+}
|
|
11
|
+
|
8
|
12
|
/**
|
9
|
13
|
* 初始化链接以及收发数据
|
10
|
14
|
*/
|
11
|
15
|
class Client {
|
12
|
16
|
private _maxPayload: number;
|
13
|
17
|
private _enableLogger: boolean;
|
14
|
|
- private listeners: Map<number, (data: string) => void>;
|
|
18
|
+ private listeners: Map<number, (data: WebSocketResp) => void>;
|
15
|
19
|
private requestHeader: string;
|
16
|
20
|
private responseHeader: string;
|
17
|
21
|
private url: string;
|
|
@@ -26,7 +30,7 @@ class Client {
|
26
|
30
|
* @param readyStateCallback 链接状态回调,可以处理onOpen、onClose、onError
|
27
|
31
|
*/
|
28
|
32
|
public constructor(url: string, readyStateCallback: ReadyStateCallback) {
|
29
|
|
- this.listeners = new Map<number, (data: string) => void>();
|
|
33
|
+ this.listeners = new Map<number, (data: WebSocketResp) => void>();
|
30
|
34
|
this.requestHeader = '';
|
31
|
35
|
this.requestHeader = '';
|
32
|
36
|
this._maxPayload = 1024 * 1024;
|
|
@@ -70,10 +74,10 @@ class Client {
|
70
|
74
|
* 发送ping请求,来保持长连接
|
71
|
75
|
* @param param 请求参数,比如{"hello":"world"}
|
72
|
76
|
*/
|
73
|
|
- public async ping(param: object): Promise<string> {
|
|
77
|
+ public async ping(param: object): Promise<WebSocketResp> {
|
74
|
78
|
return new Promise(
|
75
|
79
|
(
|
76
|
|
- resolve: (data: string) => void,
|
|
80
|
+ resolve: (data: WebSocketResp) => void,
|
77
|
81
|
reject: (err: WebsocketError) => void,
|
78
|
82
|
): void => {
|
79
|
83
|
if (this.socket.readyState !== this.socket.OPEN) {
|
|
@@ -88,7 +92,7 @@ class Client {
|
88
|
92
|
|
89
|
93
|
this.listeners.set(
|
90
|
94
|
heartbeatOperator,
|
91
|
|
- (data: string): void => {
|
|
95
|
+ (data: WebSocketResp): void => {
|
92
|
96
|
const code = this.getResponseProperty('code');
|
93
|
97
|
if (code !== '') {
|
94
|
98
|
const message = this.getResponseProperty('message');
|
|
@@ -128,7 +132,10 @@ class Client {
|
128
|
132
|
* @param param 请求参数,比如{"hello":"world"}
|
129
|
133
|
* @param callback 请求状态回调处理
|
130
|
134
|
*/
|
131
|
|
- public async request(operator: string, param: object): Promise<string> {
|
|
135
|
+ public async request(
|
|
136
|
+ operator: string,
|
|
137
|
+ param: object,
|
|
138
|
+ ): Promise<WebSocketResp> {
|
132
|
139
|
return await this.asyncSend(operator, param);
|
133
|
140
|
}
|
134
|
141
|
|
|
@@ -140,7 +147,7 @@ class Client {
|
140
|
147
|
*/
|
141
|
148
|
public addMessageListener(
|
142
|
149
|
operator: string,
|
143
|
|
- listener: (data: string) => void,
|
|
150
|
+ listener: (data: WebSocketResp) => void,
|
144
|
151
|
): void {
|
145
|
152
|
this.listeners.set(Utils.crc32(operator), listener);
|
146
|
153
|
}
|
|
@@ -220,14 +227,12 @@ class Client {
|
220
|
227
|
|
221
|
228
|
return '';
|
222
|
229
|
}
|
223
|
|
-
|
224
|
230
|
/**
|
225
|
231
|
* 关闭客户端链接
|
226
|
232
|
*/
|
227
|
233
|
public close(code?: number, reason?: string): void {
|
228
|
234
|
this.socket.close(code, reason);
|
229
|
235
|
}
|
230
|
|
-
|
231
|
236
|
/**
|
232
|
237
|
* 创建websocket链接
|
233
|
238
|
*/
|
|
@@ -287,7 +292,7 @@ class Client {
|
287
|
292
|
|
288
|
293
|
this.responseHeader = packet.header;
|
289
|
294
|
|
290
|
|
- (this.listeners.get(operator) as (data: string) => void)(
|
|
295
|
+ (this.listeners.get(operator) as (data: WebSocketResp) => void)(
|
291
|
296
|
JSON.parse(packet.body),
|
292
|
297
|
);
|
293
|
298
|
}
|
|
@@ -312,7 +317,7 @@ class Client {
|
312
|
317
|
/**
|
313
|
318
|
* 断线重连
|
314
|
319
|
*/
|
315
|
|
- private reconnect(): void {
|
|
320
|
+ reconnect(): void {
|
316
|
321
|
if (!this.reconnectLock) {
|
317
|
322
|
this.reconnectLock = true;
|
318
|
323
|
if (this._enableLogger) {
|
|
@@ -356,10 +361,10 @@ class Client {
|
356
|
361
|
* @param param 请求参数,比如{"hello":"world"}
|
357
|
362
|
* @param callback 请求状态回调处理
|
358
|
363
|
*/
|
359
|
|
- private asyncSend(operator: string, param: object): Promise<string> {
|
|
364
|
+ asyncSend(operator: string, param: object): Promise<WebSocketResp> {
|
360
|
365
|
return new Promise(
|
361
|
366
|
(
|
362
|
|
- resolve: (data: string) => void,
|
|
367
|
+ resolve: (data: WebSocketResp) => void,
|
363
|
368
|
reject: (err: WebsocketError) => void,
|
364
|
369
|
): void => {
|
365
|
370
|
if (this.socket.readyState !== this.socket.OPEN) {
|
|
@@ -376,7 +381,7 @@ class Client {
|
376
|
381
|
const listener = Utils.crc32(operator) + sequence;
|
377
|
382
|
this.listeners.set(
|
378
|
383
|
listener,
|
379
|
|
- (data: string): void => {
|
|
384
|
+ (data: WebSocketResp): void => {
|
380
|
385
|
const code = this.getResponseProperty('code');
|
381
|
386
|
if (code !== '') {
|
382
|
387
|
const message = this.getResponseProperty('message');
|
|
@@ -413,4 +418,14 @@ class Client {
|
413
|
418
|
}
|
414
|
419
|
}
|
415
|
420
|
|
416
|
|
-export { Client };
|
|
421
|
+let client: Client;
|
|
422
|
+
|
|
423
|
+function getClient(url: string, callback: ReadyStateCallback) {
|
|
424
|
+ if (!client) {
|
|
425
|
+ client = new Client(url, callback);
|
|
426
|
+ }
|
|
427
|
+
|
|
428
|
+ return client;
|
|
429
|
+}
|
|
430
|
+
|
|
431
|
+export { Client, getClient };
|