Browse Source

update:兼容RN的处理,尝试

adam 2 years ago
parent
commit
95aeccc811
3 changed files with 64 additions and 27 deletions
  1. 2
    1
      package.json
  2. 22
    26
      src/index.ts
  3. 40
    0
      yarn.lock

+ 2
- 1
package.json View File

@@ -11,7 +11,7 @@
11 11
     "test": "jest --config=scripts/jest/jest.config.js",
12 12
     "build": "npm run clean && npm run build:cjs && npm run build:umd",
13 13
     "build:cjs": "tsc --project ./tsconfig.cjs.json",
14
-    "build:umd": "NODE_ENV=production webpack --config scripts/webpack/webpack.config.umd.js",
14
+    "build:umd": "cross-env NODE_ENV=production webpack --config scripts/webpack/webpack.config.umd.js",
15 15
     "clean": "rimraf dist"
16 16
   },
17 17
   "repository": {
@@ -26,6 +26,7 @@
26 26
     "node-int64": "^0.4.0"
27 27
   },
28 28
   "devDependencies": {
29
+    "cross-env": "^7.0.3",
29 30
     "@babel/cli": "^7.4.4",
30 31
     "@babel/core": "^7.4.4",
31 32
     "@babel/plugin-proposal-class-properties": "^7.4.4",

+ 22
- 26
src/index.ts View File

@@ -287,39 +287,35 @@ class Client {
287 287
     };
288 288
 
289 289
     ws.onmessage = (ev): void => {
290
-      if (ev.data instanceof Blob) {
291
-        let reader = new FileReader();
292
-        reader.readAsArrayBuffer(ev.data);
293
-        reader.onload = (): void => {
294
-          try {
295
-            let packet = new Packet().unPack(reader.result as ArrayBuffer);
296
-            let packetLength = packet.headerLength + packet.bodyLength + 20;
297
-            if (packetLength > this._maxPayload) {
298
-              throw new Error('the packet is big than ' + this._maxPayload);
299
-            }
290
+      if (ev.data instanceof ArrayBuffer) {
291
+        try {
292
+          let packet = new Packet().unPack(ev.data);
293
+          let packetLength = packet.headerLength + packet.bodyLength + 20;
294
+          if (packetLength > this._maxPayload) {
295
+            throw new Error('the packet is big than ' + this._maxPayload);
296
+          }
300 297
 
301
-            let operator = Number(packet.operator) + Number(packet.sequence);
302
-            let listener = operator.toString();
298
+          let operator = Number(packet.operator) + Number(packet.sequence);
299
+          let listener = operator.toString();
303 300
 
304
-            this.listeners.listeners(listener).forEach((param): void => {
305
-              if (packet.body === '') {
306
-                packet.body = '{}';
307
-              }
301
+          this.listeners.listeners(listener).forEach((param): void => {
302
+            if (packet.body === '') {
303
+              packet.body = '{}';
304
+            }
308 305
 
309
-              this.responseHeader = packet.header;
306
+            this.responseHeader = packet.header;
310 307
 
311
-              (param as (data: WebSocketResp) => void)(JSON.parse(packet.body));
312
-            });
308
+            (param as (data: WebSocketResp) => void)(JSON.parse(packet.body));
309
+          });
313 310
 
314
-            if (this._enableLogger) {
315
-              if (operator !== 0 && packet.body !== 'null') {
316
-                console.info('receive data packet', packet.body);
317
-              }
311
+          if (this._enableLogger) {
312
+            if (operator !== 0 && packet.body !== 'null') {
313
+              console.info('receive data packet', packet.body);
318 314
             }
319
-          } catch (e) {
320
-            throw new Error(e);
321 315
           }
322
-        };
316
+        } catch (e) {
317
+          throw new Error(e);
318
+        }
323 319
       } else {
324 320
         throw new Error('unsupported data format');
325 321
       }

+ 40
- 0
yarn.lock View File

@@ -2386,6 +2386,13 @@ create-hmac@^1.1.0, create-hmac@^1.1.2, create-hmac@^1.1.4:
2386 2386
     safe-buffer "^5.0.1"
2387 2387
     sha.js "^2.4.8"
2388 2388
 
2389
+cross-env@^7.0.3:
2390
+  version "7.0.3"
2391
+  resolved "https://registry.nlark.com/cross-env/download/cross-env-7.0.3.tgz#865264b29677dc015ba8418918965dd232fc54cf"
2392
+  integrity sha1-hlJkspZ33AFbqEGJGJZd0jL8VM8=
2393
+  dependencies:
2394
+    cross-spawn "^7.0.1"
2395
+
2389 2396
 cross-spawn@^6.0.0, cross-spawn@^6.0.5:
2390 2397
   version "6.0.5"
2391 2398
   resolved "http://registry.npm.taobao.org/cross-spawn/download/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4"
@@ -2396,6 +2403,15 @@ cross-spawn@^6.0.0, cross-spawn@^6.0.5:
2396 2403
     shebang-command "^1.2.0"
2397 2404
     which "^1.2.9"
2398 2405
 
2406
+cross-spawn@^7.0.1:
2407
+  version "7.0.3"
2408
+  resolved "https://registry.nlark.com/cross-spawn/download/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
2409
+  integrity sha1-9zqFudXUHQRVUcF34ogtSshXKKY=
2410
+  dependencies:
2411
+    path-key "^3.1.0"
2412
+    shebang-command "^2.0.0"
2413
+    which "^2.0.1"
2414
+
2399 2415
 crypto-browserify@^3.11.0:
2400 2416
   version "3.12.0"
2401 2417
   resolved "https://registry.npm.taobao.org/crypto-browserify/download/crypto-browserify-3.12.0.tgz#396cf9f3137f03e4b8e532c58f698254e00f80ec"
@@ -5267,6 +5283,11 @@ path-key@^2.0.0, path-key@^2.0.1:
5267 5283
   version "2.0.1"
5268 5284
   resolved "http://registry.npm.taobao.org/path-key/download/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40"
5269 5285
 
5286
+path-key@^3.1.0:
5287
+  version "3.1.1"
5288
+  resolved "https://registry.npm.taobao.org/path-key/download/path-key-3.1.1.tgz#581f6ade658cbba65a0d3380de7753295054f375"
5289
+  integrity sha1-WB9q3mWMu6ZaDTOA3ndTKVBU83U=
5290
+
5270 5291
 path-parse@^1.0.6:
5271 5292
   version "1.0.6"
5272 5293
   resolved "http://registry.npm.taobao.org/path-parse/download/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c"
@@ -5974,10 +5995,22 @@ shebang-command@^1.2.0:
5974 5995
   dependencies:
5975 5996
     shebang-regex "^1.0.0"
5976 5997
 
5998
+shebang-command@^2.0.0:
5999
+  version "2.0.0"
6000
+  resolved "https://registry.npm.taobao.org/shebang-command/download/shebang-command-2.0.0.tgz#ccd0af4f8835fbdc265b82461aaf0c36663f34ea"
6001
+  integrity sha1-zNCvT4g1+9wmW4JGGq8MNmY/NOo=
6002
+  dependencies:
6003
+    shebang-regex "^3.0.0"
6004
+
5977 6005
 shebang-regex@^1.0.0:
5978 6006
   version "1.0.0"
5979 6007
   resolved "http://registry.npm.taobao.org/shebang-regex/download/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3"
5980 6008
 
6009
+shebang-regex@^3.0.0:
6010
+  version "3.0.0"
6011
+  resolved "https://registry.nlark.com/shebang-regex/download/shebang-regex-3.0.0.tgz?cache=0&sync_timestamp=1628896299850&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fshebang-regex%2Fdownload%2Fshebang-regex-3.0.0.tgz#ae16f1644d873ecad843b0307b143362d4c42172"
6012
+  integrity sha1-rhbxZE2HPsrYQ7AwexQzYtTEIXI=
6013
+
5981 6014
 shellwords@^0.1.1:
5982 6015
   version "0.1.1"
5983 6016
   resolved "http://registry.npm.taobao.org/shellwords/download/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b"
@@ -6873,6 +6906,13 @@ which@^1.2.14, which@^1.2.9, which@^1.3.0:
6873 6906
   dependencies:
6874 6907
     isexe "^2.0.0"
6875 6908
 
6909
+which@^2.0.1:
6910
+  version "2.0.2"
6911
+  resolved "https://registry.nlark.com/which/download/which-2.0.2.tgz#7c6a8dd0a636a0327e10b59c9286eee93f3f51b1"
6912
+  integrity sha1-fGqN0KY2oDJ+ELWckobu6T8/UbE=
6913
+  dependencies:
6914
+    isexe "^2.0.0"
6915
+
6876 6916
 wide-align@^1.1.0:
6877 6917
   version "1.1.3"
6878 6918
   resolved "https://registry.npm.taobao.org/wide-align/download/wide-align-1.1.3.tgz#ae074e6bdc0c14a431e804e624549c633b000457"