|
@@ -1,4 +1,4 @@
|
|
|
-import { util } from "./util";
|
|
|
+import { concatArrayBuffers, util } from "./util";
|
|
|
import logger from "./logger";
|
|
|
import { Negotiator } from "./negotiator";
|
|
|
import { ConnectionType, SerializationType, ServerMessageType } from "./enums";
|
|
@@ -65,7 +65,7 @@ export class DataConnection
|
|
|
private _buffering = false;
|
|
|
private _chunkedData: {
|
|
|
[id: number]: {
|
|
|
- data: Blob[];
|
|
|
+ data: Uint8Array[];
|
|
|
count: number;
|
|
|
total: number;
|
|
|
};
|
|
@@ -119,9 +119,7 @@ export class DataConnection
|
|
|
/** Called by the Negotiator when the DataChannel is ready. */
|
|
|
override _initializeDataChannel(dc: RTCDataChannel): void {
|
|
|
this._dc = dc;
|
|
|
- if (!util.supports.binaryBlob || util.supports.reliable) {
|
|
|
- this.dataChannel.binaryType = "arraybuffer";
|
|
|
- }
|
|
|
+ this.dataChannel.binaryType = "arraybuffer";
|
|
|
|
|
|
this.dataChannel.onopen = () => {
|
|
|
logger.log(`DC#${this.connectionId} dc connection success`);
|
|
@@ -193,7 +191,7 @@ export class DataConnection
|
|
|
__peerData: number;
|
|
|
n: number;
|
|
|
total: number;
|
|
|
- data: Blob;
|
|
|
+ data: ArrayBuffer;
|
|
|
}): void {
|
|
|
const id = data.__peerData;
|
|
|
const chunkInfo = this._chunkedData[id] || {
|
|
@@ -202,7 +200,7 @@ export class DataConnection
|
|
|
total: data.total,
|
|
|
};
|
|
|
|
|
|
- chunkInfo.data[data.n] = data.data;
|
|
|
+ chunkInfo.data[data.n] = new Uint8Array(data.data);
|
|
|
chunkInfo.count++;
|
|
|
this._chunkedData[id] = chunkInfo;
|
|
|
|
|
@@ -211,8 +209,8 @@ export class DataConnection
|
|
|
delete this._chunkedData[id];
|
|
|
|
|
|
// We've received all the chunks--time to construct the complete data.
|
|
|
- const data = new Blob(chunkInfo.data);
|
|
|
- this._handleDataMessage({ data });
|
|
|
+ const data = concatArrayBuffers(chunkInfo.data);
|
|
|
+ this.emit("data", util.unpack(data));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -283,6 +281,11 @@ export class DataConnection
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ if (data instanceof Blob) {
|
|
|
+ data.arrayBuffer().then((ab) => this.send(ab));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
if (this.serialization === SerializationType.JSON) {
|
|
|
this._bufferedSend(this.stringify(data));
|
|
|
} else if (
|
|
@@ -291,18 +294,12 @@ export class DataConnection
|
|
|
) {
|
|
|
const blob = util.pack(data);
|
|
|
|
|
|
- if (!chunked && blob.size > util.chunkedMTU) {
|
|
|
+ if (!chunked && blob.byteLength > util.chunkedMTU) {
|
|
|
this._sendChunks(blob);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- if (!util.supports.binaryBlob) {
|
|
|
- // We only do this if we really need to (e.g. blobs are not supported),
|
|
|
- // because this conversion is costly.
|
|
|
- this._encodingQueue.enque(blob);
|
|
|
- } else {
|
|
|
- this._bufferedSend(blob);
|
|
|
- }
|
|
|
+ this._bufferedSend(blob);
|
|
|
} else {
|
|
|
this._bufferedSend(data);
|
|
|
}
|
|
@@ -364,7 +361,7 @@ export class DataConnection
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private _sendChunks(blob: Blob): void {
|
|
|
+ private _sendChunks(blob: ArrayBuffer): void {
|
|
|
const blobs = util.chunk(blob);
|
|
|
logger.log(`DC#${this.connectionId} Try to send ${blobs.length} chunks...`);
|
|
|
|