Paul пре 5 година
родитељ
комит
3294f9e0c3
1 измењених фајлова са 11 додато и 7 уклоњено
  1. 11
    7
      src/index.ts

+ 11
- 7
src/index.ts Прегледај датотеку

@@ -8,10 +8,10 @@ const MAX_PAYLOAD = 1024 * 1024;
8 8
  * Client ws client, 单例模式, 负责维护连接
9 9
  */
10 10
 class Client {
11
+  private _maxPayload: number;
11 12
   private listeners: Map<number, (data: string) => void>;
12 13
   private requestHeader: string;
13 14
   private responseHeader: string;
14
-  private maxPayload: number;
15 15
   private url: string;
16 16
   private reconnectTimes: number;
17 17
   private reconnectLock: boolean;
@@ -22,7 +22,7 @@ class Client {
22 22
     this.listeners = new Map<number, (data: string) => void>();
23 23
     this.requestHeader = '';
24 24
     this.requestHeader = '';
25
-    this.maxPayload = MAX_PAYLOAD;
25
+    this._maxPayload = MAX_PAYLOAD;
26 26
     this.url = url;
27 27
     this.reconnectTimes = 0;
28 28
     this.readyStateCallback = readyStateCallback;
@@ -123,13 +123,17 @@ class Client {
123 123
   }
124 124
 
125 125
   // 获取socket的链接状态
126
-  public getReadyState() {
126
+  public getReadyState(): number {
127 127
     return this.socket.readyState;
128 128
   }
129 129
 
130 130
   // 设置单个请求能够处理的最大字节数
131
-  public setMaxPayload(maxPayload) {
132
-    this.maxPayload = maxPayload;
131
+  public set maxPayload(maxPayload: number) {
132
+    this._maxPayload = maxPayload;
133
+  }
134
+
135
+  public get maxPayload(): number {
136
+    return this._maxPayload;
133 137
   }
134 138
 
135 139
   // 设置请求属性
@@ -211,8 +215,8 @@ class Client {
211 215
           try {
212 216
             let packet = new Packet().unPack(reader.result);
213 217
             let packetLength = packet.headerLength + packet.bodyLength + 20;
214
-            if (packetLength > this.maxPayload) {
215
-              throw new Error('the packet is big than ' + this.maxPayload);
218
+            if (packetLength > this._maxPayload) {
219
+              throw new Error('the packet is big than ' + this._maxPayload);
216 220
             }
217 221
 
218 222
             let operator = Number(packet.operator) + Number(packet.sequence);