Paul 5 лет назад
Родитель
Сommit
b625123a2a
1 измененных файлов: 29 добавлений и 34 удалений
  1. 29
    34
      src/index.ts

+ 29
- 34
src/index.ts Просмотреть файл

@@ -27,32 +27,27 @@ class Client {
27 27
   }
28 28
 
29 29
   // 向服务端发送ping包保持长连接
30
-  ping(param = {}, callback = {}) {
31
-    if (typeof callback !== 'object') {
32
-      throw new Error('callback must be an object');
33
-    }
34
-
30
+  ping(param = {}, requestCallback: RequestCallback) {
35 31
     if (this.socket.readyState !== this.socket.OPEN) {
36 32
       throw new Error('asyncSend: connection refuse');
37 33
     }
38 34
 
39
-    let _this = this;
40
-    this.addMessageListener(0, function(data) {
41
-      let code = _this.getResponseProperty('code');
35
+    this.addMessageListener(0, (data) => {
36
+      let code = this.getResponseProperty('code');
42 37
       if (typeof code !== 'undefined') {
43
-        let message = _this.getResponseProperty('message');
44
-        if (this.callback.onError !== null) {
45
-          this.callback.onError(code, message);
38
+        let message = this.getResponseProperty('message');
39
+        if (requestCallback.onError !== null) {
40
+          requestCallback.onError(Number(code), message);
46 41
         }
47 42
       } else {
48
-        this.callback.onSuccess(data);
43
+        requestCallback.onSuccess(data);
49 44
       }
50 45
 
51
-      this.callback.onEnd();
46
+      requestCallback.onEnd();
52 47
     });
53 48
 
54 49
     const p = new Packet();
55
-    _this.send(p.pack(0, 0, _this.requestHeader, JSON.stringify(param)));
50
+    this.send(p.pack(0, 0, this.requestHeader, JSON.stringify(param)));
56 51
   }
57 52
 
58 53
   send(data) {
@@ -91,13 +86,12 @@ class Client {
91 86
       callback.onStart();
92 87
     }
93 88
 
94
-    let _this = this;
95 89
     let sequence = new Date().getTime();
96 90
     let listener = Utils.crc32(operator) + sequence;
97
-    this.requestCallback[listener] = function(data) {
98
-      let code = _this.getResponseProperty('code');
91
+    this.requestCallback[listener] = (data) => {
92
+      let code = this.getResponseProperty('code');
99 93
       if (typeof code !== 'undefined') {
100
-        let message = _this.getResponseProperty('message');
94
+        let message = this.getResponseProperty('message');
101 95
         if (
102 96
           callback.hasOwnProperty('onError') &&
103 97
           typeof callback.onError === 'function'
@@ -120,7 +114,7 @@ class Client {
120 114
         callback.onEnd();
121 115
       }
122 116
 
123
-      delete _this.requestCallback[listener];
117
+      delete this.requestCallback[listener];
124 118
     };
125 119
 
126 120
     const p = new Packet();
@@ -187,7 +181,7 @@ class Client {
187 181
   }
188 182
 
189 183
   // 获取响应属性
190
-  getResponseProperty(key) {
184
+  getResponseProperty(key): string {
191 185
     let values = this.responseHeader.split(';');
192 186
     for (let index in values) {
193 187
       let kv = values[index].split('=');
@@ -195,18 +189,19 @@ class Client {
195 189
         return kv[1];
196 190
       }
197 191
     }
192
+
193
+    return '';
198 194
   }
199 195
 
200 196
   // 创建连接
201 197
   connect(): WebSocket {
202 198
     const readyStateCallback = this.readyStateCallback;
203
-
204 199
     let ws = new WebSocket(this.url);
200
+
205 201
     ws.binaryType = 'blob';
206
-    let _this = this;
207 202
 
208
-    ws.onopen = function(ev) {
209
-      _this.reconnectTimes = 0;
203
+    ws.onopen = (ev) => {
204
+      this.reconnectTimes = 0;
210 205
       if (
211 206
         readyStateCallback.hasOwnProperty('onOpen') &&
212 207
         typeof readyStateCallback.onOpen === 'function'
@@ -215,8 +210,8 @@ class Client {
215 210
       }
216 211
     };
217 212
 
218
-    ws.onclose = function(ev) {
219
-      _this.reconnect();
213
+    ws.onclose = (ev) => {
214
+      this.reconnect();
220 215
       if (
221 216
         readyStateCallback.hasOwnProperty('onClose') &&
222 217
         typeof readyStateCallback.onClose === 'function'
@@ -225,8 +220,8 @@ class Client {
225 220
       }
226 221
     };
227 222
 
228
-    ws.onerror = function(ev) {
229
-      _this.reconnect();
223
+    ws.onerror = (ev) => {
224
+      this.reconnect();
230 225
       if (
231 226
         readyStateCallback.hasOwnProperty('onError') &&
232 227
         typeof readyStateCallback.onError === 'function'
@@ -235,25 +230,25 @@ class Client {
235 230
       }
236 231
     };
237 232
 
238
-    ws.onmessage = function(ev) {
233
+    ws.onmessage = (ev) => {
239 234
       if (ev.data instanceof Blob) {
240 235
         let reader = new FileReader();
241 236
         reader.readAsArrayBuffer(ev.data);
242
-        reader.onload = function() {
237
+        reader.onload = () => {
243 238
           try {
244
-            let packet = new Packet().unPack(this.result);
239
+            let packet = new Packet().unPack(reader.result);
245 240
             let packetLength = packet.headerLength + packet.bodyLength + 20;
246 241
             if (packetLength > MAX_PAYLOAD) {
247 242
               throw new Error('the packet is big than ' + MAX_PAYLOAD);
248 243
             }
249 244
 
250 245
             let operator = Number(packet.operator) + Number(packet.sequence);
251
-            if (_this.requestCallback.hasOwnProperty(operator)) {
246
+            if (this.requestCallback.hasOwnProperty(operator)) {
252 247
               if (packet.body === '') {
253 248
                 packet.body = '{}';
254 249
               }
255
-              _this.responseHeader = packet.header;
256
-              _this.requestCallback[operator](JSON.parse(packet.body));
250
+              this.responseHeader = packet.header;
251
+              this.requestCallback[operator](JSON.parse(packet.body));
257 252
             }
258 253
             if (operator !== 0 && packet.body !== 'null') {
259 254
               console.info('receive data', packet.body);