Browse Source

fix asynSend

Paul 5 years ago
parent
commit
c98396d5bc
2 changed files with 36 additions and 38 deletions
  1. 22
    0
      src/example/main.ts
  2. 14
    38
      src/index.ts

+ 22
- 0
src/example/main.ts View File

@@ -25,6 +25,28 @@ const client = new Client(
25 25
           }
26 26
         })(),
27 27
       );
28
+
29
+      client.syncSend(
30
+        '/v1/healthy',
31
+        undefined,
32
+        new (class {
33
+          onStart(): void {
34
+            console.log('start request');
35
+          }
36
+
37
+          onSuccess(data: string): void {
38
+            console.log('request successful:', data);
39
+          }
40
+
41
+          onError(code: number, message: string): void {
42
+            console.log('request error:', message);
43
+          }
44
+
45
+          onEnd(): void {
46
+            console.log('end request');
47
+          }
48
+        })(),
49
+      );
28 50
     }
29 51
 
30 52
     onClose(ev: Event) {

+ 14
- 38
src/index.ts View File

@@ -71,54 +71,30 @@ class Client {
71 71
    * @param {*} param
72 72
    * @param {*} callback 仅此次有效的callback
73 73
    */
74
-  asyncSend(operator, param, callback) {
74
+  asyncSend(operator: string, param: any, callback: RequestCallback) {
75 75
     console.info('websocket send data', operator, this.requestHeader, param);
76 76
 
77
-    if (typeof callback !== 'object') {
78
-      throw new Error('callback must be an object');
79
-    }
80
-
81 77
     if (this.socket.readyState !== this.socket.OPEN) {
82 78
       throw new Error('asyncSend: connection refuse');
83 79
     }
84 80
 
85
-    if (
86
-      callback.hasOwnProperty('onStart') &&
87
-      typeof callback.onStart === 'function'
88
-    ) {
89
-      callback.onStart();
90
-    }
81
+    callback.onStart();
91 82
 
92
-    let sequence = new Date().getTime();
93
-    let listener = Utils.crc32(operator) + sequence;
94
-    this.requestCallback[listener] = (data) => {
95
-      let code = this.getResponseProperty('code');
96
-      if (typeof code !== 'undefined') {
97
-        let message = this.getResponseProperty('message');
98
-        if (
99
-          callback.hasOwnProperty('onError') &&
100
-          typeof callback.onError === 'function'
101
-        ) {
102
-          callback.onError(code, message);
103
-        }
83
+    const sequence = new Date().getTime();
84
+    const listener = Utils.crc32(operator) + sequence;
85
+    this.listeners.set(listener, (data: string) => {
86
+      const code = this.getResponseProperty('code');
87
+      if (code !== '') {
88
+        const message = this.getResponseProperty('message');
89
+        callback.onError(Number(code), message);
104 90
       } else {
105
-        if (
106
-          callback.hasOwnProperty('onSuccess') &&
107
-          typeof callback.onSuccess === 'function'
108
-        ) {
109
-          callback.onSuccess(data);
110
-        }
91
+        callback.onSuccess(data);
111 92
       }
112 93
 
113
-      if (
114
-        callback.hasOwnProperty('onEnd') &&
115
-        typeof callback.onEnd === 'function'
116
-      ) {
117
-        callback.onEnd();
118
-      }
94
+      callback.onEnd();
119 95
 
120
-      delete this.requestCallback[listener];
121
-    };
96
+      delete this.listeners[listener];
97
+    });
122 98
 
123 99
     const p = new Packet();
124 100
     this.send(
@@ -132,7 +108,7 @@ class Client {
132 108
   }
133 109
 
134 110
   // 同步请求服务端数据
135
-  async syncSend(operator, param, callback) {
111
+  async syncSend(operator: string, param: any, callback: RequestCallback) {
136 112
     await this.asyncSend(operator, param, callback);
137 113
   }
138 114