Browse Source

add eventemmit

Paul 5 years ago
parent
commit
d72f7b8900
4 changed files with 23 additions and 17 deletions
  1. 1
    1
      example/main.ts
  2. 4
    3
      package.json
  3. 14
    13
      src/index.ts
  4. 4
    0
      yarn.lock

+ 1
- 1
example/main.ts View File

1
 import { Client } from '../src/';
1
 import { Client } from '../src/';
2
 import { WebsocketError, WebSocketResp } from '../src/types';
2
 import { WebsocketError, WebSocketResp } from '../src/types';
3
 
3
 
4
-const url = 'ws://127.0.0.1:8081';
4
+const url = 'ws://127.0.0.1:8085';
5
 const client = Client.getInstance(url, {
5
 const client = Client.getInstance(url, {
6
   onOpen(): void {
6
   onOpen(): void {
7
     client
7
     client

+ 4
- 3
package.json View File

1
 {
1
 {
2
   "name": "ts-linker-sdk",
2
   "name": "ts-linker-sdk",
3
-  "version": "1.0.6",
3
+  "version": "1.0.7",
4
   "description": "linker's ts sdk",
4
   "description": "linker's ts sdk",
5
   "main": "dist/index.js",
5
   "main": "dist/index.js",
6
   "types": "dist/types/index.d.ts",
6
   "types": "dist/types/index.d.ts",
22
   "license": "private",
22
   "license": "private",
23
   "dependencies": {
23
   "dependencies": {
24
     "crypto-js": "^3.1.9-1",
24
     "crypto-js": "^3.1.9-1",
25
+    "eventemitter3": "^4.0.0",
25
     "node-int64": "^0.4.0"
26
     "node-int64": "^0.4.0"
26
   },
27
   },
27
   "devDependencies": {
28
   "devDependencies": {
47
     "eslint-loader": "^2.1.2",
48
     "eslint-loader": "^2.1.2",
48
     "html-webpack-plugin": "^3.2.0",
49
     "html-webpack-plugin": "^3.2.0",
49
     "jest": "^24.5.0",
50
     "jest": "^24.5.0",
50
-    "webpack-dev-server": "^3.3.1",
51
     "rimraf": "^2.6.3",
51
     "rimraf": "^2.6.3",
52
     "ts-jest": "^24.0.0",
52
     "ts-jest": "^24.0.0",
53
     "typescript": "3.5",
53
     "typescript": "3.5",
54
     "webpack": "^4.30.0",
54
     "webpack": "^4.30.0",
55
-    "webpack-cli": "^3.3.2"
55
+    "webpack-cli": "^3.3.2",
56
+    "webpack-dev-server": "^3.3.1"
56
   },
57
   },
57
   "files": [
58
   "files": [
58
     "dist/"
59
     "dist/"

+ 14
- 13
src/index.ts View File

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

+ 4
- 0
yarn.lock View File

2919
   version "3.1.2"
2919
   version "3.1.2"
2920
   resolved "https://registry.npm.taobao.org/eventemitter3/download/eventemitter3-3.1.2.tgz#2d3d48f9c346698fce83a85d7d664e98535df6e7"
2920
   resolved "https://registry.npm.taobao.org/eventemitter3/download/eventemitter3-3.1.2.tgz#2d3d48f9c346698fce83a85d7d664e98535df6e7"
2921
 
2921
 
2922
+eventemitter3@^4.0.0:
2923
+  version "4.0.0"
2924
+  resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.0.tgz#d65176163887ee59f386d64c82610b696a4a74eb"
2925
+
2922
 events@^3.0.0:
2926
 events@^3.0.0:
2923
   version "3.0.0"
2927
   version "3.0.0"
2924
   resolved "https://registry.npm.taobao.org/events/download/events-3.0.0.tgz#9a0a0dfaf62893d92b875b8f2698ca4114973e88"
2928
   resolved "https://registry.npm.taobao.org/events/download/events-3.0.0.tgz#9a0a0dfaf62893d92b875b8f2698ca4114973e88"