adam 3 years ago
parent
commit
3d098b5d98
3 changed files with 12222 additions and 7 deletions
  1. 12205
    0
      package-lock.json
  2. 5
    5
      package.json
  3. 12
    2
      src/index.ts

+ 12205
- 0
package-lock.json
File diff suppressed because it is too large
View File


+ 5
- 5
package.json View File

@@ -1,6 +1,6 @@
1 1
 {
2
-  "name": "ts-linker-sdk",
3
-  "version": "1.0.7",
2
+  "name": "linker-sdk",
3
+  "version": "1.0.0",
4 4
   "description": "linker's ts sdk",
5 5
   "main": "dist/index.js",
6 6
   "types": "dist/types/index.d.ts",
@@ -16,10 +16,10 @@
16 16
   },
17 17
   "repository": {
18 18
     "type": "git",
19
-    "url": "https://github.com/wpajqz/ts-sdk.git"
19
+    "url": "https://github.com/bilingo-com/ts-sdk"
20 20
   },
21
-  "author": "Paul",
22
-  "license": "private",
21
+  "author": "adam_fu",
22
+  "license": "MIT",
23 23
   "dependencies": {
24 24
     "crypto-js": "^3.1.9-1",
25 25
     "eventemitter3": "^4.0.0",

+ 12
- 2
src/index.ts View File

@@ -287,9 +287,9 @@ class Client {
287 287
     };
288 288
 
289 289
     ws.onmessage = (ev): void => {
290
-      if (ev.data instanceof ArrayBuffer) {
290
+      const handleData = (data: ArrayBuffer) => {
291 291
         try {
292
-          let packet = new Packet().unPack(ev.data);
292
+          let packet = new Packet().unPack(data);
293 293
           let packetLength = packet.headerLength + packet.bodyLength + 20;
294 294
           if (packetLength > this._maxPayload) {
295 295
             throw new Error('the packet is big than ' + this._maxPayload);
@@ -316,6 +316,16 @@ class Client {
316 316
         } catch (e) {
317 317
           throw new Error(e);
318 318
         }
319
+      }
320
+      // 区分不同类型数据的解析(暂时支持Blob和ArrayBuffer)
321
+      if (ev.data instanceof Blob) {
322
+        let reader = new FileReader();
323
+        reader.readAsArrayBuffer(ev.data);
324
+        reader.onload = (): void => {
325
+          handleData(reader.result as ArrayBuffer)
326
+        };
327
+      } else if(ev.data instanceof ArrayBuffer) {
328
+        handleData(ev.data);
319 329
       } else {
320 330
         throw new Error('unsupported data format');
321 331
       }