Browse Source

delete _this

Paul 5 years ago
parent
commit
b625123a2a
1 changed files with 29 additions and 34 deletions
  1. 29
    34
      src/index.ts

+ 29
- 34
src/index.ts View File

27
   }
27
   }
28
 
28
 
29
   // 向服务端发送ping包保持长连接
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
     if (this.socket.readyState !== this.socket.OPEN) {
31
     if (this.socket.readyState !== this.socket.OPEN) {
36
       throw new Error('asyncSend: connection refuse');
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
       if (typeof code !== 'undefined') {
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
       } else {
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
     const p = new Packet();
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
   send(data) {
53
   send(data) {
91
       callback.onStart();
86
       callback.onStart();
92
     }
87
     }
93
 
88
 
94
-    let _this = this;
95
     let sequence = new Date().getTime();
89
     let sequence = new Date().getTime();
96
     let listener = Utils.crc32(operator) + sequence;
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
       if (typeof code !== 'undefined') {
93
       if (typeof code !== 'undefined') {
100
-        let message = _this.getResponseProperty('message');
94
+        let message = this.getResponseProperty('message');
101
         if (
95
         if (
102
           callback.hasOwnProperty('onError') &&
96
           callback.hasOwnProperty('onError') &&
103
           typeof callback.onError === 'function'
97
           typeof callback.onError === 'function'
120
         callback.onEnd();
114
         callback.onEnd();
121
       }
115
       }
122
 
116
 
123
-      delete _this.requestCallback[listener];
117
+      delete this.requestCallback[listener];
124
     };
118
     };
125
 
119
 
126
     const p = new Packet();
120
     const p = new Packet();
187
   }
181
   }
188
 
182
 
189
   // 获取响应属性
183
   // 获取响应属性
190
-  getResponseProperty(key) {
184
+  getResponseProperty(key): string {
191
     let values = this.responseHeader.split(';');
185
     let values = this.responseHeader.split(';');
192
     for (let index in values) {
186
     for (let index in values) {
193
       let kv = values[index].split('=');
187
       let kv = values[index].split('=');
195
         return kv[1];
189
         return kv[1];
196
       }
190
       }
197
     }
191
     }
192
+
193
+    return '';
198
   }
194
   }
199
 
195
 
200
   // 创建连接
196
   // 创建连接
201
   connect(): WebSocket {
197
   connect(): WebSocket {
202
     const readyStateCallback = this.readyStateCallback;
198
     const readyStateCallback = this.readyStateCallback;
203
-
204
     let ws = new WebSocket(this.url);
199
     let ws = new WebSocket(this.url);
200
+
205
     ws.binaryType = 'blob';
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
       if (
205
       if (
211
         readyStateCallback.hasOwnProperty('onOpen') &&
206
         readyStateCallback.hasOwnProperty('onOpen') &&
212
         typeof readyStateCallback.onOpen === 'function'
207
         typeof readyStateCallback.onOpen === 'function'
215
       }
210
       }
216
     };
211
     };
217
 
212
 
218
-    ws.onclose = function(ev) {
219
-      _this.reconnect();
213
+    ws.onclose = (ev) => {
214
+      this.reconnect();
220
       if (
215
       if (
221
         readyStateCallback.hasOwnProperty('onClose') &&
216
         readyStateCallback.hasOwnProperty('onClose') &&
222
         typeof readyStateCallback.onClose === 'function'
217
         typeof readyStateCallback.onClose === 'function'
225
       }
220
       }
226
     };
221
     };
227
 
222
 
228
-    ws.onerror = function(ev) {
229
-      _this.reconnect();
223
+    ws.onerror = (ev) => {
224
+      this.reconnect();
230
       if (
225
       if (
231
         readyStateCallback.hasOwnProperty('onError') &&
226
         readyStateCallback.hasOwnProperty('onError') &&
232
         typeof readyStateCallback.onError === 'function'
227
         typeof readyStateCallback.onError === 'function'
235
       }
230
       }
236
     };
231
     };
237
 
232
 
238
-    ws.onmessage = function(ev) {
233
+    ws.onmessage = (ev) => {
239
       if (ev.data instanceof Blob) {
234
       if (ev.data instanceof Blob) {
240
         let reader = new FileReader();
235
         let reader = new FileReader();
241
         reader.readAsArrayBuffer(ev.data);
236
         reader.readAsArrayBuffer(ev.data);
242
-        reader.onload = function() {
237
+        reader.onload = () => {
243
           try {
238
           try {
244
-            let packet = new Packet().unPack(this.result);
239
+            let packet = new Packet().unPack(reader.result);
245
             let packetLength = packet.headerLength + packet.bodyLength + 20;
240
             let packetLength = packet.headerLength + packet.bodyLength + 20;
246
             if (packetLength > MAX_PAYLOAD) {
241
             if (packetLength > MAX_PAYLOAD) {
247
               throw new Error('the packet is big than ' + MAX_PAYLOAD);
242
               throw new Error('the packet is big than ' + MAX_PAYLOAD);
248
             }
243
             }
249
 
244
 
250
             let operator = Number(packet.operator) + Number(packet.sequence);
245
             let operator = Number(packet.operator) + Number(packet.sequence);
251
-            if (_this.requestCallback.hasOwnProperty(operator)) {
246
+            if (this.requestCallback.hasOwnProperty(operator)) {
252
               if (packet.body === '') {
247
               if (packet.body === '') {
253
                 packet.body = '{}';
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
             if (operator !== 0 && packet.body !== 'null') {
253
             if (operator !== 0 && packet.body !== 'null') {
259
               console.info('receive data', packet.body);
254
               console.info('receive data', packet.body);