Browse Source

add types

Paul 5 years ago
parent
commit
6034ecb873
6 changed files with 32 additions and 23 deletions
  1. 2
    0
      package.json
  2. 0
    5
      src/constant.ts
  3. 18
    17
      src/index.ts
  4. 1
    1
      src/packet.ts
  5. 1
    0
      src/version.ts
  6. 10
    0
      yarn.lock

+ 2
- 0
package.json View File

15
     "node-int64": "^0.4.0"
15
     "node-int64": "^0.4.0"
16
   },
16
   },
17
   "devDependencies": {
17
   "devDependencies": {
18
+    "@types/crypto-js": "^3.1.43",
18
     "@types/jest": "^24.0.11",
19
     "@types/jest": "^24.0.11",
20
+    "@types/node-int64": "^0.4.29",
19
     "html-webpack-plugin": "^3.2.0",
21
     "html-webpack-plugin": "^3.2.0",
20
     "jest": "^24.5.0",
22
     "jest": "^24.5.0",
21
     "ts-jest": "^24.0.0",
23
     "ts-jest": "^24.0.0",

+ 0
- 5
src/constant.ts View File

1
-// 单个数据包能够传输的最大数据量
2
-export const MAX_PAYLOAD = 1024 * 1024;
3
-
4
-// 版本
5
-export const VERSION = '1.0';

+ 18
- 17
src/index.ts View File

1
-import * as constant from './constant';
2
 import { ReadyStateCallback, RequestCallback } from './callback';
1
 import { ReadyStateCallback, RequestCallback } from './callback';
3
 import { Packet } from './packet';
2
 import { Packet } from './packet';
4
 import { Utils } from './utils';
3
 import { Utils } from './utils';
5
 
4
 
5
+const MAX_PAYLOAD = 1024 * 1024;
6
+
6
 /**
7
 /**
7
  * Client ws client, 单例模式, 负责维护连接
8
  * Client ws client, 单例模式, 负责维护连接
8
  */
9
  */
9
-export class Client {
10
+class Client {
10
   private requestCallback: RequestCallback;
11
   private requestCallback: RequestCallback;
11
   private requestHeader: string;
12
   private requestHeader: string;
12
   private responseHeader: string;
13
   private responseHeader: string;
18
   private readyStateCallback: ReadyStateCallback;
19
   private readyStateCallback: ReadyStateCallback;
19
 
20
 
20
   constructor(url: string, readyStateCallback: ReadyStateCallback) {
21
   constructor(url: string, readyStateCallback: ReadyStateCallback) {
21
-    this.maxPayload = constant.MAX_PAYLOAD;
22
+    this.maxPayload = MAX_PAYLOAD;
22
     this.url = url;
23
     this.url = url;
23
     this.readyStateCallback = readyStateCallback;
24
     this.readyStateCallback = readyStateCallback;
24
 
25
 
36
     }
37
     }
37
 
38
 
38
     let _this = this;
39
     let _this = this;
39
-    this.addMessageListener(0, function (data) {
40
+    this.addMessageListener(0, function(data) {
40
       let code = _this.getResponseProperty('code');
41
       let code = _this.getResponseProperty('code');
41
       if (typeof code !== 'undefined') {
42
       if (typeof code !== 'undefined') {
42
         let message = _this.getResponseProperty('message');
43
         let message = _this.getResponseProperty('message');
93
     let _this = this;
94
     let _this = this;
94
     let sequence = new Date().getTime();
95
     let sequence = new Date().getTime();
95
     let listener = Utils.crc32(operator) + sequence;
96
     let listener = Utils.crc32(operator) + sequence;
96
-    this.requestCallback[listener] = function (data) {
97
+    this.requestCallback[listener] = function(data) {
97
       let code = _this.getResponseProperty('code');
98
       let code = _this.getResponseProperty('code');
98
       if (typeof code !== 'undefined') {
99
       if (typeof code !== 'undefined') {
99
         let message = _this.getResponseProperty('message');
100
         let message = _this.getResponseProperty('message');
197
   }
198
   }
198
 
199
 
199
   // 创建连接
200
   // 创建连接
200
-  connect() {
201
-    const url = this.url;
201
+  connect(): WebSocket {
202
     const readyStateCallback = this.readyStateCallback;
202
     const readyStateCallback = this.readyStateCallback;
203
 
203
 
204
-    let ws = new WebSocket(url);
205
-    let _this = this;
206
-
204
+    let ws = new WebSocket(this.url);
207
     ws.binaryType = 'blob';
205
     ws.binaryType = 'blob';
206
+    let _this = this;
208
 
207
 
209
-    ws.onopen = function (ev) {
208
+    ws.onopen = function(ev) {
210
       _this.reconnectTimes = 0;
209
       _this.reconnectTimes = 0;
211
       if (
210
       if (
212
         readyStateCallback.hasOwnProperty('onOpen') &&
211
         readyStateCallback.hasOwnProperty('onOpen') &&
216
       }
215
       }
217
     };
216
     };
218
 
217
 
219
-    ws.onclose = function (ev) {
218
+    ws.onclose = function(ev) {
220
       _this.reconnect();
219
       _this.reconnect();
221
       if (
220
       if (
222
         readyStateCallback.hasOwnProperty('onClose') &&
221
         readyStateCallback.hasOwnProperty('onClose') &&
226
       }
225
       }
227
     };
226
     };
228
 
227
 
229
-    ws.onerror = function (ev) {
228
+    ws.onerror = function(ev) {
230
       _this.reconnect();
229
       _this.reconnect();
231
       if (
230
       if (
232
         readyStateCallback.hasOwnProperty('onError') &&
231
         readyStateCallback.hasOwnProperty('onError') &&
236
       }
235
       }
237
     };
236
     };
238
 
237
 
239
-    ws.onmessage = function (ev) {
238
+    ws.onmessage = function(ev) {
240
       if (ev.data instanceof Blob) {
239
       if (ev.data instanceof Blob) {
241
         let reader = new FileReader();
240
         let reader = new FileReader();
242
         reader.readAsArrayBuffer(ev.data);
241
         reader.readAsArrayBuffer(ev.data);
243
-        reader.onload = function () {
242
+        reader.onload = function() {
244
           try {
243
           try {
245
             let packet = new Packet().unPack(this.result);
244
             let packet = new Packet().unPack(this.result);
246
             let packetLength = packet.headerLength + packet.bodyLength + 20;
245
             let packetLength = packet.headerLength + packet.bodyLength + 20;
247
-            if (packetLength > constant.MAX_PAYLOAD) {
248
-              throw new Error('the packet is big than ' + constant.MAX_PAYLOAD);
246
+            if (packetLength > MAX_PAYLOAD) {
247
+              throw new Error('the packet is big than ' + MAX_PAYLOAD);
249
             }
248
             }
250
 
249
 
251
             let operator = Number(packet.operator) + Number(packet.sequence);
250
             let operator = Number(packet.operator) + Number(packet.sequence);
285
     }
284
     }
286
   }
285
   }
287
 }
286
 }
287
+
288
+export { Client, MAX_PAYLOAD };

+ 1
- 1
src/packet.ts View File

1
 import { Utils } from './utils';
1
 import { Utils } from './utils';
2
-import * as Int64 from 'node-int64';
2
+import Int64 = require('node-int64');
3
 
3
 
4
 export class Packet {
4
 export class Packet {
5
   private key: string = 'b8ca9aa66def05ff3f24919274bb4a66';
5
   private key: string = 'b8ca9aa66def05ff3f24919274bb4a66';

+ 1
- 0
src/version.ts View File

1
+export const VERSION = '1.0';

+ 10
- 0
yarn.lock View File

281
   dependencies:
281
   dependencies:
282
     "@babel/types" "^7.3.0"
282
     "@babel/types" "^7.3.0"
283
 
283
 
284
+"@types/crypto-js@^3.1.43":
285
+  version "3.1.43"
286
+  resolved "https://registry.npm.taobao.org/@types/crypto-js/download/@types/crypto-js-3.1.43.tgz#b859347d6289ba13e347c335a4c9efa63337a748"
287
+
284
 "@types/events@*":
288
 "@types/events@*":
285
   version "3.0.0"
289
   version "3.0.0"
286
   resolved "https://registry.npm.taobao.org/@types/events/download/@types/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7"
290
   resolved "https://registry.npm.taobao.org/@types/events/download/@types/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7"
311
   version "3.0.3"
315
   version "3.0.3"
312
   resolved "https://registry.npm.taobao.org/@types/minimatch/download/@types/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
316
   resolved "https://registry.npm.taobao.org/@types/minimatch/download/@types/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
313
 
317
 
318
+"@types/node-int64@^0.4.29":
319
+  version "0.4.29"
320
+  resolved "https://registry.npm.taobao.org/@types/node-int64/download/@types/node-int64-0.4.29.tgz#8c7c16a7c1195ae4f8beaa903b0018ac66291d16"
321
+  dependencies:
322
+    "@types/node" "*"
323
+
314
 "@types/node@*":
324
 "@types/node@*":
315
   version "11.11.6"
325
   version "11.11.6"
316
   resolved "http://registry.npm.taobao.org/@types/node/download/@types/node-11.11.6.tgz#df929d1bb2eee5afdda598a41930fe50b43eaa6a"
326
   resolved "http://registry.npm.taobao.org/@types/node/download/@types/node-11.11.6.tgz#df929d1bb2eee5afdda598a41930fe50b43eaa6a"