Browse Source

add access modifer

Paul 5 years ago
parent
commit
93dfd8018b
1 changed files with 32 additions and 25 deletions
  1. 32
    25
      src/index.ts

+ 32
- 25
src/index.ts View File

30
   }
30
   }
31
 
31
 
32
   // 向服务端发送ping包保持长连接
32
   // 向服务端发送ping包保持长连接
33
-  ping(param: any, requestCallback: RequestCallback) {
33
+  public ping(param: any, requestCallback: RequestCallback) {
34
     if (this.socket.readyState !== this.socket.OPEN) {
34
     if (this.socket.readyState !== this.socket.OPEN) {
35
       throw new Error('asyncSend: connection refuse');
35
       throw new Error('asyncSend: connection refuse');
36
     }
36
     }
58
     );
58
     );
59
   }
59
   }
60
 
60
 
61
-  send(data: ArrayBuffer) {
62
-    if (this.socket.readyState !== this.socket.OPEN) {
63
-      console.error('WebSocket is already in CLOSING or CLOSED state.');
64
-      return;
65
-    }
66
-    try {
67
-      this.socket.send(data);
68
-    } catch (e) {
69
-      console.log('send data error', e);
70
-    }
71
-  }
72
-
73
   /**
61
   /**
74
    * asyncSend
62
    * asyncSend
75
    * @param {*} operator
63
    * @param {*} operator
76
    * @param {*} param
64
    * @param {*} param
77
    * @param {*} callback 仅此次有效的callback
65
    * @param {*} callback 仅此次有效的callback
78
    */
66
    */
79
-  asyncSend(operator: string, param: any, callback: RequestCallback) {
67
+  public asyncSend(operator: string, param: any, callback: RequestCallback) {
80
     console.info('websocket send data', operator, this.requestHeader, param);
68
     console.info('websocket send data', operator, this.requestHeader, param);
81
 
69
 
82
     if (this.socket.readyState !== this.socket.OPEN) {
70
     if (this.socket.readyState !== this.socket.OPEN) {
113
   }
101
   }
114
 
102
 
115
   // 同步请求服务端数据
103
   // 同步请求服务端数据
116
-  async syncSend(operator: string, param: any, callback: RequestCallback) {
104
+  public async syncSend(
105
+    operator: string,
106
+    param: any,
107
+    callback: RequestCallback,
108
+  ) {
117
     await this.asyncSend(operator, param, callback);
109
     await this.asyncSend(operator, param, callback);
118
   }
110
   }
119
 
111
 
120
   // 添加消息监听
112
   // 添加消息监听
121
-  addMessageListener(operator: string, listener: (data: string) => void) {
113
+  public addMessageListener(
114
+    operator: string,
115
+    listener: (data: string) => void,
116
+  ) {
122
     this.listeners[Utils.crc32(operator)] = listener;
117
     this.listeners[Utils.crc32(operator)] = listener;
123
   }
118
   }
124
 
119
 
125
   // 移除消息监听
120
   // 移除消息监听
126
-  removeMessageListener(operator: string) {
121
+  public removeMessageListener(operator: string) {
127
     delete this.listeners[Utils.crc32(operator)];
122
     delete this.listeners[Utils.crc32(operator)];
128
   }
123
   }
129
 
124
 
130
   // 获取socket的链接状态
125
   // 获取socket的链接状态
131
-  getReadyState() {
126
+  public getReadyState() {
132
     return this.socket.readyState;
127
     return this.socket.readyState;
133
   }
128
   }
134
 
129
 
135
   // 设置单个请求能够处理的最大字节数
130
   // 设置单个请求能够处理的最大字节数
136
-  setMaxPayload(maxPayload) {
131
+  public setMaxPayload(maxPayload) {
137
     this.maxPayload = maxPayload;
132
     this.maxPayload = maxPayload;
138
   }
133
   }
139
 
134
 
140
   // 设置请求属性
135
   // 设置请求属性
141
-  setRequestProperty(key: string, value: string) {
136
+  public setRequestProperty(key: string, value: string) {
142
     let v = this.getRequestProperty(key);
137
     let v = this.getRequestProperty(key);
143
 
138
 
144
     this.requestHeader = this.requestHeader.replace(key + '=' + v + ';', '');
139
     this.requestHeader = this.requestHeader.replace(key + '=' + v + ';', '');
146
   }
141
   }
147
 
142
 
148
   // 获取请求属性
143
   // 获取请求属性
149
-  getRequestProperty(key: string): string {
144
+  public getRequestProperty(key: string): string {
150
     if (this.requestHeader !== undefined) {
145
     if (this.requestHeader !== undefined) {
151
       let values = this.requestHeader.split(';');
146
       let values = this.requestHeader.split(';');
152
       for (let index in values) {
147
       for (let index in values) {
161
   }
156
   }
162
 
157
 
163
   // 设置Response属性
158
   // 设置Response属性
164
-  setResponseProperty(key: string, value: string) {
159
+  public setResponseProperty(key: string, value: string) {
165
     let v = this.getResponseProperty(key);
160
     let v = this.getResponseProperty(key);
166
 
161
 
167
     this.responseHeader = this.responseHeader.replace(key + '=' + v + ';', '');
162
     this.responseHeader = this.responseHeader.replace(key + '=' + v + ';', '');
169
   }
164
   }
170
 
165
 
171
   // 获取响应属性
166
   // 获取响应属性
172
-  getResponseProperty(key: string): string {
167
+  public getResponseProperty(key: string): string {
173
     if (this.responseHeader !== undefined) {
168
     if (this.responseHeader !== undefined) {
174
       let values = this.responseHeader.split(';');
169
       let values = this.responseHeader.split(';');
175
       for (let index in values) {
170
       for (let index in values) {
184
   }
179
   }
185
 
180
 
186
   // 创建连接
181
   // 创建连接
187
-  connect(): WebSocket {
182
+  private connect(): WebSocket {
188
     const readyStateCallback = this.readyStateCallback;
183
     const readyStateCallback = this.readyStateCallback;
189
     let ws = new WebSocket(this.url);
184
     let ws = new WebSocket(this.url);
190
 
185
 
246
     return ws;
241
     return ws;
247
   }
242
   }
248
 
243
 
249
-  reconnect() {
244
+  private reconnect() {
250
     if (!this.reconnectLock) {
245
     if (!this.reconnectLock) {
251
       this.reconnectLock = true;
246
       this.reconnectLock = true;
252
       console.info('websocket reconnect in ' + this.reconnectTimes + 's');
247
       console.info('websocket reconnect in ' + this.reconnectTimes + 's');
258
       }, this.reconnectTimes * 1000);
253
       }, this.reconnectTimes * 1000);
259
     }
254
     }
260
   }
255
   }
256
+
257
+  private send(data: ArrayBuffer) {
258
+    if (this.socket.readyState !== this.socket.OPEN) {
259
+      console.error('WebSocket is already in CLOSING or CLOSED state.');
260
+      return;
261
+    }
262
+    try {
263
+      this.socket.send(data);
264
+    } catch (e) {
265
+      console.log('send data error', e);
266
+    }
267
+  }
261
 }
268
 }
262
 
269
 
263
 export { Client, MAX_PAYLOAD };
270
 export { Client, MAX_PAYLOAD };