Browse Source

order method

Paul 5 years ago
parent
commit
48f4a81016
1 changed files with 78 additions and 78 deletions
  1. 78
    78
      src/client.ts

+ 78
- 78
src/client.ts View File

@@ -10,6 +10,7 @@ const clientError = 400;
10 10
  */
11 11
 class Client {
12 12
   private _maxPayload: number;
13
+  private _enableLogger: boolean;
13 14
   private listeners: Map<number, (data: string) => void>;
14 15
   private requestHeader: string;
15 16
   private responseHeader: string;
@@ -18,7 +19,6 @@ class Client {
18 19
   private reconnectLock: boolean;
19 20
   private socket: WebSocket;
20 21
   private readyStateCallback: ReadyStateCallback;
21
-  private _enableLogger: boolean;
22 22
 
23 23
   /**
24 24
    * 构造函数,初始化客户端链接
@@ -37,6 +37,21 @@ class Client {
37 37
     this.socket = this.connect();
38 38
   }
39 39
 
40
+  /**
41
+   * 设置可以处理的数据包上限
42
+   * @param maxPayload 最多可以处理的数据包大小
43
+   */
44
+  public set maxPayload(maxPayload: number) {
45
+    this._maxPayload = maxPayload;
46
+  }
47
+
48
+  /**
49
+   * 获取可以处理的数据包大小
50
+   */
51
+  public get maxPayload(): number {
52
+    return this._maxPayload;
53
+  }
54
+
40 55
   /**
41 56
    * 设置是否允许显示运行日志
42 57
    */
@@ -107,68 +122,6 @@ class Client {
107 122
     );
108 123
   }
109 124
 
110
-  /**
111
-   * 异步向服务端发送请求
112
-   * @param operator 路由地址
113
-   * @param param 请求参数,比如{"hello":"world"}
114
-   * @param callback 请求状态回调处理
115
-   */
116
-  private asyncSend(operator: string, param: object): Promise<string> {
117
-    return new Promise(
118
-      (
119
-        resolve: (data: string) => void,
120
-        reject: (err: WebsocketError) => void,
121
-      ): void => {
122
-        if (this.socket.readyState !== this.socket.OPEN) {
123
-          if (this._enableLogger) {
124
-            console.log('[ping]: connection refuse');
125
-          }
126
-
127
-          reject(
128
-            new WebsocketError(clientError, 'asyncSend: connection refuse'),
129
-          );
130
-        }
131
-
132
-        const sequence = new Date().getTime();
133
-        const listener = Utils.crc32(operator) + sequence;
134
-        this.listeners.set(
135
-          listener,
136
-          (data: string): void => {
137
-            const code = this.getResponseProperty('code');
138
-            if (code !== '') {
139
-              const message = this.getResponseProperty('message');
140
-              reject(new WebsocketError(Number(code), message));
141
-            } else {
142
-              resolve(data);
143
-            }
144
-
145
-            delete this.listeners[listener];
146
-          },
147
-        );
148
-
149
-        const p = new Packet();
150
-        this.send(
151
-          p.pack(
152
-            Utils.crc32(operator),
153
-            sequence,
154
-            this.requestHeader,
155
-            JSON.stringify(param),
156
-          ),
157
-        );
158
-
159
-        if (this._enableLogger) {
160
-          console.info(
161
-            '[send data packet]',
162
-            operator,
163
-            sequence,
164
-            this.requestHeader,
165
-            param,
166
-          );
167
-        }
168
-      },
169
-    );
170
-  }
171
-
172 125
   /**
173 126
    * 同步方式向服务端发送请求
174 127
    * @param operator 路由地址
@@ -208,21 +161,6 @@ class Client {
208 161
     return this.socket.readyState;
209 162
   }
210 163
 
211
-  /**
212
-   * 设置可以处理的数据包上限
213
-   * @param maxPayload 最多可以处理的数据包大小
214
-   */
215
-  public set maxPayload(maxPayload: number) {
216
-    this._maxPayload = maxPayload;
217
-  }
218
-
219
-  /**
220
-   * 获取可以处理的数据包大小
221
-   */
222
-  public get maxPayload(): number {
223
-    return this._maxPayload;
224
-  }
225
-
226 164
   /**
227 165
    * 添加请求属性,会携带在数据帧里面发送到服务端
228 166
    * @param key 属性名
@@ -404,6 +342,68 @@ class Client {
404 342
       throw new Error('send data error' + e);
405 343
     }
406 344
   }
345
+
346
+  /**
347
+   * 异步向服务端发送请求
348
+   * @param operator 路由地址
349
+   * @param param 请求参数,比如{"hello":"world"}
350
+   * @param callback 请求状态回调处理
351
+   */
352
+  private asyncSend(operator: string, param: object): Promise<string> {
353
+    return new Promise(
354
+      (
355
+        resolve: (data: string) => void,
356
+        reject: (err: WebsocketError) => void,
357
+      ): void => {
358
+        if (this.socket.readyState !== this.socket.OPEN) {
359
+          if (this._enableLogger) {
360
+            console.log('[ping]: connection refuse');
361
+          }
362
+
363
+          reject(
364
+            new WebsocketError(clientError, 'asyncSend: connection refuse'),
365
+          );
366
+        }
367
+
368
+        const sequence = new Date().getTime();
369
+        const listener = Utils.crc32(operator) + sequence;
370
+        this.listeners.set(
371
+          listener,
372
+          (data: string): void => {
373
+            const code = this.getResponseProperty('code');
374
+            if (code !== '') {
375
+              const message = this.getResponseProperty('message');
376
+              reject(new WebsocketError(Number(code), message));
377
+            } else {
378
+              resolve(data);
379
+            }
380
+
381
+            delete this.listeners[listener];
382
+          },
383
+        );
384
+
385
+        const p = new Packet();
386
+        this.send(
387
+          p.pack(
388
+            Utils.crc32(operator),
389
+            sequence,
390
+            this.requestHeader,
391
+            JSON.stringify(param),
392
+          ),
393
+        );
394
+
395
+        if (this._enableLogger) {
396
+          console.info(
397
+            '[send data packet]',
398
+            operator,
399
+            sequence,
400
+            this.requestHeader,
401
+            param,
402
+          );
403
+        }
404
+      },
405
+    );
406
+  }
407 407
 }
408 408
 
409 409
 export { Client };