|
@@ -7,27 +7,20 @@ import { Utils } from './utils';
|
7
|
7
|
* Client ws client, 单例模式, 负责维护连接
|
8
|
8
|
*/
|
9
|
9
|
export class Client {
|
10
|
|
- public callback: callback;
|
11
|
|
- public requestHeader: string;
|
12
|
|
- public responseHeader: string;
|
13
|
|
- public maxPayload: number;
|
14
|
|
- public url: string;
|
15
|
|
- public reconnectTimes: number;
|
16
|
|
- public reconnectLock: boolean;
|
17
|
|
- public socket: WebSocket;
|
18
|
|
- public readyStateCallback: readyStateCallback;
|
|
10
|
+ private requestCallback: callback;
|
|
11
|
+ private requestHeader: string;
|
|
12
|
+ private responseHeader: string;
|
|
13
|
+ private maxPayload: number;
|
|
14
|
+ private url: string;
|
|
15
|
+ private reconnectTimes: number;
|
|
16
|
+ private reconnectLock: boolean;
|
|
17
|
+ private socket: WebSocket;
|
|
18
|
+ private readyStateCallback: readyStateCallback;
|
19
|
19
|
|
20
|
20
|
constructor(url: string, readyStateCallback: readyStateCallback) {
|
21
|
|
- if (!('WebSocket' in window)) {
|
22
|
|
- return;
|
23
|
|
- }
|
24
|
|
-
|
25
|
|
- this.requestHeader = '';
|
26
|
21
|
this.maxPayload = constant.MAX_PAYLOAD;
|
27
|
22
|
this.url = url;
|
28
|
23
|
this.readyStateCallback = readyStateCallback;
|
29
|
|
- this.reconnectTimes = 0;
|
30
|
|
- this.reconnectLock = false;
|
31
|
24
|
|
32
|
25
|
this.socket = this.connect();
|
33
|
26
|
}
|
|
@@ -100,7 +93,7 @@ export class Client {
|
100
|
93
|
let _this = this;
|
101
|
94
|
let sequence = new Date().getTime();
|
102
|
95
|
let listener = Utils.crc32(operator) + sequence;
|
103
|
|
- this.callback[listener] = function(data) {
|
|
96
|
+ this.requestCallback[listener] = function(data) {
|
104
|
97
|
let code = _this.getResponseProperty('code');
|
105
|
98
|
if (typeof code !== 'undefined') {
|
106
|
99
|
let message = _this.getResponseProperty('message');
|
|
@@ -126,7 +119,7 @@ export class Client {
|
126
|
119
|
callback.onEnd();
|
127
|
120
|
}
|
128
|
121
|
|
129
|
|
- delete _this.callback[listener];
|
|
122
|
+ delete _this.requestCallback[listener];
|
130
|
123
|
};
|
131
|
124
|
|
132
|
125
|
const p = new Packet();
|
|
@@ -147,12 +140,12 @@ export class Client {
|
147
|
140
|
|
148
|
141
|
// 添加消息监听
|
149
|
142
|
addMessageListener(operator, listener) {
|
150
|
|
- this.callback[Utils.crc32(operator)] = listener;
|
|
143
|
+ this.requestCallback[Utils.crc32(operator)] = listener;
|
151
|
144
|
}
|
152
|
145
|
|
153
|
146
|
// 移除消息监听
|
154
|
147
|
removeMessageListener(operator) {
|
155
|
|
- delete this.callback[Utils.crc32(operator)];
|
|
148
|
+ delete this.requestCallback[Utils.crc32(operator)];
|
156
|
149
|
}
|
157
|
150
|
|
158
|
151
|
// 获取socket的链接状态
|
|
@@ -217,10 +210,10 @@ export class Client {
|
217
|
210
|
console.info('websocket connected');
|
218
|
211
|
_this.reconnectTimes = 0;
|
219
|
212
|
if (
|
220
|
|
- readyStateCallback.hasOwnProperty('onopen') &&
|
221
|
|
- typeof readyStateCallback.onopen === 'function'
|
|
213
|
+ readyStateCallback.hasOwnProperty('onOpen') &&
|
|
214
|
+ typeof readyStateCallback.onOpen === 'function'
|
222
|
215
|
) {
|
223
|
|
- readyStateCallback.onopen(ev);
|
|
216
|
+ readyStateCallback.onOpen(ev);
|
224
|
217
|
}
|
225
|
218
|
};
|
226
|
219
|
|
|
@@ -228,10 +221,10 @@ export class Client {
|
228
|
221
|
console.info('websocket disconnected');
|
229
|
222
|
_this.reconnect();
|
230
|
223
|
if (
|
231
|
|
- readyStateCallback.hasOwnProperty('onclose') &&
|
232
|
|
- typeof readyStateCallback.onclose === 'function'
|
|
224
|
+ readyStateCallback.hasOwnProperty('onClose') &&
|
|
225
|
+ typeof readyStateCallback.onClose === 'function'
|
233
|
226
|
) {
|
234
|
|
- readyStateCallback.onclose(ev);
|
|
227
|
+ readyStateCallback.onClose(ev);
|
235
|
228
|
}
|
236
|
229
|
};
|
237
|
230
|
|
|
@@ -239,10 +232,10 @@ export class Client {
|
239
|
232
|
console.info('websocket error disconnected');
|
240
|
233
|
_this.reconnect();
|
241
|
234
|
if (
|
242
|
|
- readyStateCallback.hasOwnProperty('onerror') &&
|
243
|
|
- typeof readyStateCallback.onerror === 'function'
|
|
235
|
+ readyStateCallback.hasOwnProperty('onError') &&
|
|
236
|
+ typeof readyStateCallback.onError === 'function'
|
244
|
237
|
) {
|
245
|
|
- readyStateCallback.onerror(ev);
|
|
238
|
+ readyStateCallback.onError(ev);
|
246
|
239
|
}
|
247
|
240
|
};
|
248
|
241
|
|
|
@@ -259,12 +252,12 @@ export class Client {
|
259
|
252
|
}
|
260
|
253
|
|
261
|
254
|
let operator = Number(packet.operator) + Number(packet.sequence);
|
262
|
|
- if (_this.callback.hasOwnProperty(operator)) {
|
|
255
|
+ if (_this.requestCallback.hasOwnProperty(operator)) {
|
263
|
256
|
if (packet.body === '') {
|
264
|
257
|
packet.body = '{}';
|
265
|
258
|
}
|
266
|
259
|
_this.responseHeader = packet.header;
|
267
|
|
- _this.callback[operator](JSON.parse(packet.body));
|
|
260
|
+ _this.requestCallback[operator](JSON.parse(packet.body));
|
268
|
261
|
}
|
269
|
262
|
if (operator !== 0 && packet.body !== 'null') {
|
270
|
263
|
console.info('receive data', packet.body);
|