|
@@ -1,12 +1,13 @@
|
1
|
|
-import * as constant from './constant';
|
2
|
1
|
import { ReadyStateCallback, RequestCallback } from './callback';
|
3
|
2
|
import { Packet } from './packet';
|
4
|
3
|
import { Utils } from './utils';
|
5
|
4
|
|
|
5
|
+const MAX_PAYLOAD = 1024 * 1024;
|
|
6
|
+
|
6
|
7
|
/**
|
7
|
8
|
* Client ws client, 单例模式, 负责维护连接
|
8
|
9
|
*/
|
9
|
|
-export class Client {
|
|
10
|
+class Client {
|
10
|
11
|
private requestCallback: RequestCallback;
|
11
|
12
|
private requestHeader: string;
|
12
|
13
|
private responseHeader: string;
|
|
@@ -18,7 +19,7 @@ export class Client {
|
18
|
19
|
private readyStateCallback: ReadyStateCallback;
|
19
|
20
|
|
20
|
21
|
constructor(url: string, readyStateCallback: ReadyStateCallback) {
|
21
|
|
- this.maxPayload = constant.MAX_PAYLOAD;
|
|
22
|
+ this.maxPayload = MAX_PAYLOAD;
|
22
|
23
|
this.url = url;
|
23
|
24
|
this.readyStateCallback = readyStateCallback;
|
24
|
25
|
|
|
@@ -36,7 +37,7 @@ export class Client {
|
36
|
37
|
}
|
37
|
38
|
|
38
|
39
|
let _this = this;
|
39
|
|
- this.addMessageListener(0, function (data) {
|
|
40
|
+ this.addMessageListener(0, function(data) {
|
40
|
41
|
let code = _this.getResponseProperty('code');
|
41
|
42
|
if (typeof code !== 'undefined') {
|
42
|
43
|
let message = _this.getResponseProperty('message');
|
|
@@ -93,7 +94,7 @@ export class Client {
|
93
|
94
|
let _this = this;
|
94
|
95
|
let sequence = new Date().getTime();
|
95
|
96
|
let listener = Utils.crc32(operator) + sequence;
|
96
|
|
- this.requestCallback[listener] = function (data) {
|
|
97
|
+ this.requestCallback[listener] = function(data) {
|
97
|
98
|
let code = _this.getResponseProperty('code');
|
98
|
99
|
if (typeof code !== 'undefined') {
|
99
|
100
|
let message = _this.getResponseProperty('message');
|
|
@@ -197,16 +198,14 @@ export class Client {
|
197
|
198
|
}
|
198
|
199
|
|
199
|
200
|
// 创建连接
|
200
|
|
- connect() {
|
201
|
|
- const url = this.url;
|
|
201
|
+ connect(): WebSocket {
|
202
|
202
|
const readyStateCallback = this.readyStateCallback;
|
203
|
203
|
|
204
|
|
- let ws = new WebSocket(url);
|
205
|
|
- let _this = this;
|
206
|
|
-
|
|
204
|
+ let ws = new WebSocket(this.url);
|
207
|
205
|
ws.binaryType = 'blob';
|
|
206
|
+ let _this = this;
|
208
|
207
|
|
209
|
|
- ws.onopen = function (ev) {
|
|
208
|
+ ws.onopen = function(ev) {
|
210
|
209
|
_this.reconnectTimes = 0;
|
211
|
210
|
if (
|
212
|
211
|
readyStateCallback.hasOwnProperty('onOpen') &&
|
|
@@ -216,7 +215,7 @@ export class Client {
|
216
|
215
|
}
|
217
|
216
|
};
|
218
|
217
|
|
219
|
|
- ws.onclose = function (ev) {
|
|
218
|
+ ws.onclose = function(ev) {
|
220
|
219
|
_this.reconnect();
|
221
|
220
|
if (
|
222
|
221
|
readyStateCallback.hasOwnProperty('onClose') &&
|
|
@@ -226,7 +225,7 @@ export class Client {
|
226
|
225
|
}
|
227
|
226
|
};
|
228
|
227
|
|
229
|
|
- ws.onerror = function (ev) {
|
|
228
|
+ ws.onerror = function(ev) {
|
230
|
229
|
_this.reconnect();
|
231
|
230
|
if (
|
232
|
231
|
readyStateCallback.hasOwnProperty('onError') &&
|
|
@@ -236,16 +235,16 @@ export class Client {
|
236
|
235
|
}
|
237
|
236
|
};
|
238
|
237
|
|
239
|
|
- ws.onmessage = function (ev) {
|
|
238
|
+ ws.onmessage = function(ev) {
|
240
|
239
|
if (ev.data instanceof Blob) {
|
241
|
240
|
let reader = new FileReader();
|
242
|
241
|
reader.readAsArrayBuffer(ev.data);
|
243
|
|
- reader.onload = function () {
|
|
242
|
+ reader.onload = function() {
|
244
|
243
|
try {
|
245
|
244
|
let packet = new Packet().unPack(this.result);
|
246
|
245
|
let packetLength = packet.headerLength + packet.bodyLength + 20;
|
247
|
|
- if (packetLength > constant.MAX_PAYLOAD) {
|
248
|
|
- throw new Error('the packet is big than ' + constant.MAX_PAYLOAD);
|
|
246
|
+ if (packetLength > MAX_PAYLOAD) {
|
|
247
|
+ throw new Error('the packet is big than ' + MAX_PAYLOAD);
|
249
|
248
|
}
|
250
|
249
|
|
251
|
250
|
let operator = Number(packet.operator) + Number(packet.sequence);
|
|
@@ -285,3 +284,5 @@ export class Client {
|
285
|
284
|
}
|
286
|
285
|
}
|
287
|
286
|
}
|
|
287
|
+
|
|
288
|
+export { Client, MAX_PAYLOAD };
|