Browse Source

getters/setters

Paul 5 years ago
parent
commit
3294f9e0c3
1 changed files with 11 additions and 7 deletions
  1. 11
    7
      src/index.ts

+ 11
- 7
src/index.ts View File

8
  * Client ws client, 单例模式, 负责维护连接
8
  * Client ws client, 单例模式, 负责维护连接
9
  */
9
  */
10
 class Client {
10
 class Client {
11
+  private _maxPayload: number;
11
   private listeners: Map<number, (data: string) => void>;
12
   private listeners: Map<number, (data: string) => void>;
12
   private requestHeader: string;
13
   private requestHeader: string;
13
   private responseHeader: string;
14
   private responseHeader: string;
14
-  private maxPayload: number;
15
   private url: string;
15
   private url: string;
16
   private reconnectTimes: number;
16
   private reconnectTimes: number;
17
   private reconnectLock: boolean;
17
   private reconnectLock: boolean;
22
     this.listeners = new Map<number, (data: string) => void>();
22
     this.listeners = new Map<number, (data: string) => void>();
23
     this.requestHeader = '';
23
     this.requestHeader = '';
24
     this.requestHeader = '';
24
     this.requestHeader = '';
25
-    this.maxPayload = MAX_PAYLOAD;
25
+    this._maxPayload = MAX_PAYLOAD;
26
     this.url = url;
26
     this.url = url;
27
     this.reconnectTimes = 0;
27
     this.reconnectTimes = 0;
28
     this.readyStateCallback = readyStateCallback;
28
     this.readyStateCallback = readyStateCallback;
123
   }
123
   }
124
 
124
 
125
   // 获取socket的链接状态
125
   // 获取socket的链接状态
126
-  public getReadyState() {
126
+  public getReadyState(): number {
127
     return this.socket.readyState;
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
           try {
215
           try {
212
             let packet = new Packet().unPack(reader.result);
216
             let packet = new Packet().unPack(reader.result);
213
             let packetLength = packet.headerLength + packet.bodyLength + 20;
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
             let operator = Number(packet.operator) + Number(packet.sequence);
222
             let operator = Number(packet.operator) + Number(packet.sequence);