|
@@ -30,7 +30,7 @@ class Client {
|
30
|
30
|
}
|
31
|
31
|
|
32
|
32
|
// 向服务端发送ping包保持长连接
|
33
|
|
- ping(param: any, requestCallback: RequestCallback) {
|
|
33
|
+ public ping(param: any, requestCallback: RequestCallback) {
|
34
|
34
|
if (this.socket.readyState !== this.socket.OPEN) {
|
35
|
35
|
throw new Error('asyncSend: connection refuse');
|
36
|
36
|
}
|
|
@@ -58,25 +58,13 @@ class Client {
|
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
|
62
|
* asyncSend
|
75
|
63
|
* @param {*} operator
|
76
|
64
|
* @param {*} param
|
77
|
65
|
* @param {*} callback 仅此次有效的callback
|
78
|
66
|
*/
|
79
|
|
- asyncSend(operator: string, param: any, callback: RequestCallback) {
|
|
67
|
+ public asyncSend(operator: string, param: any, callback: RequestCallback) {
|
80
|
68
|
console.info('websocket send data', operator, this.requestHeader, param);
|
81
|
69
|
|
82
|
70
|
if (this.socket.readyState !== this.socket.OPEN) {
|
|
@@ -113,32 +101,39 @@ class Client {
|
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
|
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
|
117
|
this.listeners[Utils.crc32(operator)] = listener;
|
123
|
118
|
}
|
124
|
119
|
|
125
|
120
|
// 移除消息监听
|
126
|
|
- removeMessageListener(operator: string) {
|
|
121
|
+ public removeMessageListener(operator: string) {
|
127
|
122
|
delete this.listeners[Utils.crc32(operator)];
|
128
|
123
|
}
|
129
|
124
|
|
130
|
125
|
// 获取socket的链接状态
|
131
|
|
- getReadyState() {
|
|
126
|
+ public getReadyState() {
|
132
|
127
|
return this.socket.readyState;
|
133
|
128
|
}
|
134
|
129
|
|
135
|
130
|
// 设置单个请求能够处理的最大字节数
|
136
|
|
- setMaxPayload(maxPayload) {
|
|
131
|
+ public setMaxPayload(maxPayload) {
|
137
|
132
|
this.maxPayload = maxPayload;
|
138
|
133
|
}
|
139
|
134
|
|
140
|
135
|
// 设置请求属性
|
141
|
|
- setRequestProperty(key: string, value: string) {
|
|
136
|
+ public setRequestProperty(key: string, value: string) {
|
142
|
137
|
let v = this.getRequestProperty(key);
|
143
|
138
|
|
144
|
139
|
this.requestHeader = this.requestHeader.replace(key + '=' + v + ';', '');
|
|
@@ -146,7 +141,7 @@ class Client {
|
146
|
141
|
}
|
147
|
142
|
|
148
|
143
|
// 获取请求属性
|
149
|
|
- getRequestProperty(key: string): string {
|
|
144
|
+ public getRequestProperty(key: string): string {
|
150
|
145
|
if (this.requestHeader !== undefined) {
|
151
|
146
|
let values = this.requestHeader.split(';');
|
152
|
147
|
for (let index in values) {
|
|
@@ -161,7 +156,7 @@ class Client {
|
161
|
156
|
}
|
162
|
157
|
|
163
|
158
|
// 设置Response属性
|
164
|
|
- setResponseProperty(key: string, value: string) {
|
|
159
|
+ public setResponseProperty(key: string, value: string) {
|
165
|
160
|
let v = this.getResponseProperty(key);
|
166
|
161
|
|
167
|
162
|
this.responseHeader = this.responseHeader.replace(key + '=' + v + ';', '');
|
|
@@ -169,7 +164,7 @@ class Client {
|
169
|
164
|
}
|
170
|
165
|
|
171
|
166
|
// 获取响应属性
|
172
|
|
- getResponseProperty(key: string): string {
|
|
167
|
+ public getResponseProperty(key: string): string {
|
173
|
168
|
if (this.responseHeader !== undefined) {
|
174
|
169
|
let values = this.responseHeader.split(';');
|
175
|
170
|
for (let index in values) {
|
|
@@ -184,7 +179,7 @@ class Client {
|
184
|
179
|
}
|
185
|
180
|
|
186
|
181
|
// 创建连接
|
187
|
|
- connect(): WebSocket {
|
|
182
|
+ private connect(): WebSocket {
|
188
|
183
|
const readyStateCallback = this.readyStateCallback;
|
189
|
184
|
let ws = new WebSocket(this.url);
|
190
|
185
|
|
|
@@ -246,7 +241,7 @@ class Client {
|
246
|
241
|
return ws;
|
247
|
242
|
}
|
248
|
243
|
|
249
|
|
- reconnect() {
|
|
244
|
+ private reconnect() {
|
250
|
245
|
if (!this.reconnectLock) {
|
251
|
246
|
this.reconnectLock = true;
|
252
|
247
|
console.info('websocket reconnect in ' + this.reconnectTimes + 's');
|
|
@@ -258,6 +253,18 @@ class Client {
|
258
|
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
|
270
|
export { Client, MAX_PAYLOAD };
|