浏览代码

add:增加ws的BinaryType配置

adam 3 年前
父节点
当前提交
fc251f3f94
共有 2 个文件被更改,包括 9 次插入7 次删除
  1. 3
    3
      package.json
  2. 6
    4
      src/index.ts

+ 3
- 3
package.json 查看文件

@@ -1,8 +1,8 @@
1 1
 {
2
-  "name": "linker-sdk",
3
-  "version": "1.0.0",
2
+  "name": "@bilingo.com/ts-linker-sdk",
3
+  "version": "1.0.3",
4 4
   "description": "linker's ts sdk",
5
-  "main": "dist/index.js",
5
+  "main": "dist/umd/index.js",
6 6
   "types": "dist/types/index.d.ts",
7 7
   "scripts": {
8 8
     "type-check": "tsc --noEmit --project ./tsconfig.cjs.json",

+ 6
- 4
src/index.ts 查看文件

@@ -17,6 +17,7 @@ class Client {
17 17
   private _enableLogger: boolean;
18 18
   private static instance: Client;
19 19
   private listeners: EventEmitter;
20
+  private binaryType: BinaryType;
20 21
   private requestHeader: string;
21 22
   private responseHeader: string;
22 23
   private url: string;
@@ -30,7 +31,8 @@ class Client {
30 31
    * @param url websocket链接地址
31 32
    * @param readyStateCallback 链接状态回调,可以处理onOpen、onClose、onError
32 33
    */
33
-  private constructor(url: string, readyStateCallback: ReadyStateCallback) {
34
+  private constructor(url: string, readyStateCallback: ReadyStateCallback, binaryType?: BinaryType) {
35
+    this.binaryType = binaryType || 'blob';
34 36
     this.listeners = new EventEmitter();
35 37
     this.requestHeader = '';
36 38
     this.requestHeader = '';
@@ -47,9 +49,9 @@ class Client {
47 49
    * @param url websocket链接地址
48 50
    * @param readyStateCallback 链接状态回调,可以处理onOpen、onClose、onError
49 51
    */
50
-  public static getInstance(url: string, callback: ReadyStateCallback): Client {
52
+  public static getInstance(url: string, callback: ReadyStateCallback, binaryType?: BinaryType): Client {
51 53
     if (!Client.instance) {
52
-      Client.instance = new Client(url, callback);
54
+      Client.instance = new Client(url, callback, binaryType);
53 55
     }
54 56
 
55 57
     return Client.instance;
@@ -254,7 +256,7 @@ class Client {
254 256
     const readyStateCallback = this.readyStateCallback;
255 257
     let ws = new WebSocket(this.url);
256 258
 
257
-    ws.binaryType = 'blob';
259
+    ws.binaryType = this.binaryType;
258 260
 
259 261
     ws.onopen = (ev): void => {
260 262
       if (this._enableLogger) {