Pārlūkot izejas kodu

chore: update client

wxyyxc1992 5 gadus atpakaļ
vecāks
revīzija
82cc6206a0
5 mainītis faili ar 59 papildinājumiem un 29 dzēšanām
  1. 11
    4
      package.json
  2. 30
    15
      src/client.ts
  3. 3
    0
      src/index.ts
  4. 11
    0
      tsconfig.cjs.json
  5. 4
    10
      tsconfig.json

+ 11
- 4
package.json Parādīt failu

@@ -2,13 +2,16 @@
2 2
   "name": "ts-linker-sdk",
3 3
   "version": "1.0.5",
4 4
   "description": "linker's ts sdk",
5
-  "main": "lib/src/client.js",
5
+  "main": "dist/index.js",
6
+  "types": "dist/types/index.d.ts",
6 7
   "scripts": {
7 8
     "type-check": "tsc --noEmit",
8 9
     "type-check:watch": "npm run type-check -- --watch",
9 10
     "start": "webpack-dev-server --mode development",
10 11
     "test": "jest",
11
-    "build": "tsc"
12
+    "build": "npm run clean && npm run build:cjs",
13
+    "build:cjs": "tsc --project ./tsconfig.cjs.json",
14
+    "clean": "rimraf dist"
12 15
   },
13 16
   "repository": {
14 17
     "type": "git",
@@ -18,7 +21,8 @@
18 21
   "license": "private",
19 22
   "dependencies": {
20 23
     "crypto-js": "^3.1.9-1",
21
-    "node-int64": "^0.4.0"
24
+    "node-int64": "^0.4.0",
25
+    "rimraf": "^2.6.3"
22 26
   },
23 27
   "devDependencies": {
24 28
     "@babel/cli": "^7.4.4",
@@ -48,5 +52,8 @@
48 52
     "webpack": "^4.30.0",
49 53
     "webpack-cli": "^3.3.2",
50 54
     "webpack-dev-server": "^3.3.1"
51
-  }
55
+  },
56
+  "files": [
57
+    "dist/"
58
+  ]
52 59
 }

+ 30
- 15
src/client.ts Parādīt failu

@@ -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 };

+ 3
- 0
src/index.ts Parādīt failu

@@ -0,0 +1,3 @@
1
+export { WebSocketClient } from './WebSocketClient';
2
+export { WebsocketError } from './error';
3
+export { Client } from './client';

+ 11
- 0
tsconfig.cjs.json Parādīt failu

@@ -0,0 +1,11 @@
1
+{
2
+  "extends": "./tsconfig.json",
3
+  "compilerOptions": {
4
+    "module": "commonjs",
5
+    "rootDir": "./src",
6
+    "outDir": "dist/cjs",
7
+    "declaration": true,
8
+    "declarationDir": "dist/types",
9
+    "sourceMap": false
10
+  }
11
+}

+ 4
- 10
tsconfig.json Parādīt failu

@@ -6,21 +6,15 @@
6 6
     // Search under node_modules for non-relative imports.
7 7
     "moduleResolution": "node",
8 8
     // Process & infer types from .js files.
9
-    "allowJs": true,
9
+    "allowJs": false,
10 10
     // Import non-ES modules as default imports.
11 11
     "esModuleInterop": true,
12 12
     // Enable strictest settings like strictNullChecks & noImplicitAny.
13 13
     "strictNullChecks": true,
14 14
     "alwaysStrict": true,
15 15
     "sourceMap": true,
16
-    "rootDirs": [
17
-      "src",
18
-      "example"
19
-    ],
20
-    "outDir": "lib/",
16
+    "rootDirs": ["src", "example"],
17
+    "outDir": "lib/"
21 18
   },
22
-  "include": [
23
-    "src/**/*",
24
-    "example/**/*"
25
-  ]
19
+  "include": ["src/**/*", "example/**/*"]
26 20
 }