Browse Source

Merge pull request #1 from wx-chevalier/master

chore: update client
Paul 5 years ago
parent
commit
8a97caf035
No account linked to committer's email address
5 changed files with 59 additions and 29 deletions
  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 View File

2
   "name": "ts-linker-sdk",
2
   "name": "ts-linker-sdk",
3
   "version": "1.0.5",
3
   "version": "1.0.5",
4
   "description": "linker's ts sdk",
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
   "scripts": {
7
   "scripts": {
7
     "type-check": "tsc --noEmit",
8
     "type-check": "tsc --noEmit",
8
     "type-check:watch": "npm run type-check -- --watch",
9
     "type-check:watch": "npm run type-check -- --watch",
9
     "start": "webpack-dev-server --mode development",
10
     "start": "webpack-dev-server --mode development",
10
     "test": "jest",
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
   "repository": {
16
   "repository": {
14
     "type": "git",
17
     "type": "git",
18
   "license": "private",
21
   "license": "private",
19
   "dependencies": {
22
   "dependencies": {
20
     "crypto-js": "^3.1.9-1",
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
   "devDependencies": {
27
   "devDependencies": {
24
     "@babel/cli": "^7.4.4",
28
     "@babel/cli": "^7.4.4",
48
     "webpack": "^4.30.0",
52
     "webpack": "^4.30.0",
49
     "webpack-cli": "^3.3.2",
53
     "webpack-cli": "^3.3.2",
50
     "webpack-dev-server": "^3.3.1"
54
     "webpack-dev-server": "^3.3.1"
51
-  }
55
+  },
56
+  "files": [
57
+    "dist/"
58
+  ]
52
 }
59
 }

+ 30
- 15
src/client.ts View File

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

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

+ 11
- 0
tsconfig.cjs.json View File

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 View File

6
     // Search under node_modules for non-relative imports.
6
     // Search under node_modules for non-relative imports.
7
     "moduleResolution": "node",
7
     "moduleResolution": "node",
8
     // Process & infer types from .js files.
8
     // Process & infer types from .js files.
9
-    "allowJs": true,
9
+    "allowJs": false,
10
     // Import non-ES modules as default imports.
10
     // Import non-ES modules as default imports.
11
     "esModuleInterop": true,
11
     "esModuleInterop": true,
12
     // Enable strictest settings like strictNullChecks & noImplicitAny.
12
     // Enable strictest settings like strictNullChecks & noImplicitAny.
13
     "strictNullChecks": true,
13
     "strictNullChecks": true,
14
     "alwaysStrict": true,
14
     "alwaysStrict": true,
15
     "sourceMap": true,
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
 }