Bläddra i källkod

add eventemmit

Paul 5 år sedan
förälder
incheckning
d72f7b8900
4 ändrade filer med 23 tillägg och 17 borttagningar
  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 Visa fil

@@ -1,7 +1,7 @@
1 1
 import { Client } from '../src/';
2 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 5
 const client = Client.getInstance(url, {
6 6
   onOpen(): void {
7 7
     client

+ 4
- 3
package.json Visa fil

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

+ 14
- 13
src/index.ts Visa fil

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

+ 4
- 0
yarn.lock Visa fil

@@ -2919,6 +2919,10 @@ eventemitter3@^3.0.0:
2919 2919
   version "3.1.2"
2920 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 2926
 events@^3.0.0:
2923 2927
   version "3.0.0"
2924 2928
   resolved "https://registry.npm.taobao.org/events/download/events-3.0.0.tgz#9a0a0dfaf62893d92b875b8f2698ca4114973e88"