Paul vor 5 Jahren
Ursprung
Commit
6034ecb873
6 geänderte Dateien mit 32 neuen und 23 gelöschten Zeilen
  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 Datei anzeigen

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

+ 0
- 5
src/constant.ts Datei anzeigen

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

+ 18
- 17
src/index.ts Datei anzeigen

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

+ 1
- 1
src/packet.ts Datei anzeigen

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

+ 1
- 0
src/version.ts Datei anzeigen

@@ -0,0 +1 @@
1
+export const VERSION = '1.0';

+ 10
- 0
yarn.lock Datei anzeigen

@@ -281,6 +281,10 @@
281 281
   dependencies:
282 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 288
 "@types/events@*":
285 289
   version "3.0.0"
286 290
   resolved "https://registry.npm.taobao.org/@types/events/download/@types/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7"
@@ -311,6 +315,12 @@
311 315
   version "3.0.3"
312 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 324
 "@types/node@*":
315 325
   version "11.11.6"
316 326
   resolved "http://registry.npm.taobao.org/@types/node/download/@types/node-11.11.6.tgz#df929d1bb2eee5afdda598a41930fe50b43eaa6a"