|
@@ -287,9 +287,9 @@ class Client {
|
287
|
287
|
};
|
288
|
288
|
|
289
|
289
|
ws.onmessage = (ev): void => {
|
290
|
|
- if (ev.data instanceof ArrayBuffer) {
|
|
290
|
+ const handleData = (data: ArrayBuffer) => {
|
291
|
291
|
try {
|
292
|
|
- let packet = new Packet().unPack(ev.data);
|
|
292
|
+ let packet = new Packet().unPack(data);
|
293
|
293
|
let packetLength = packet.headerLength + packet.bodyLength + 20;
|
294
|
294
|
if (packetLength > this._maxPayload) {
|
295
|
295
|
throw new Error('the packet is big than ' + this._maxPayload);
|
|
@@ -316,6 +316,16 @@ class Client {
|
316
|
316
|
} catch (e) {
|
317
|
317
|
throw new Error(e);
|
318
|
318
|
}
|
|
319
|
+ }
|
|
320
|
+ // 区分不同类型数据的解析(暂时支持Blob和ArrayBuffer)
|
|
321
|
+ if (ev.data instanceof Blob) {
|
|
322
|
+ let reader = new FileReader();
|
|
323
|
+ reader.readAsArrayBuffer(ev.data);
|
|
324
|
+ reader.onload = (): void => {
|
|
325
|
+ handleData(reader.result as ArrayBuffer)
|
|
326
|
+ };
|
|
327
|
+ } else if(ev.data instanceof ArrayBuffer) {
|
|
328
|
+ handleData(ev.data);
|
319
|
329
|
} else {
|
320
|
330
|
throw new Error('unsupported data format');
|
321
|
331
|
}
|