Fix bug with loading multiple assets at once

This commit is contained in:
Mitchell McCaffrey 2020-12-12 17:36:56 +11:00
parent bdba2ecc13
commit 12d9e64461

View File

@ -11,6 +11,7 @@ import { logError } from "../helpers/logging";
* @property {string} id - The socket id of the peer * @property {string} id - The socket id of the peer
* @property {Connection} connection - The actual peer connection * @property {Connection} connection - The actual peer connection
* @property {boolean} initiator - Is this peer the initiator of the connection * @property {boolean} initiator - Is this peer the initiator of the connection
* @property {boolean} ready - Ready for data to be sent
*/ */
/** /**
@ -119,6 +120,9 @@ class Session extends EventEmitter {
sendTo(sessionId, eventId, data, channel) { sendTo(sessionId, eventId, data, channel) {
if (!(sessionId in this.peers)) { if (!(sessionId in this.peers)) {
this._addPeer(sessionId, true); this._addPeer(sessionId, true);
}
if (!this.peers[sessionId].ready) {
this.peers[sessionId].connection.once("connect", () => { this.peers[sessionId].connection.once("connect", () => {
this.peers[sessionId].connection.send({ id: eventId, data }, channel); this.peers[sessionId].connection.send({ id: eventId, data }, channel);
}); });
@ -137,6 +141,9 @@ class Session extends EventEmitter {
startStreamTo(sessionId, track, stream) { startStreamTo(sessionId, track, stream) {
if (!(sessionId in this.peers)) { if (!(sessionId in this.peers)) {
this._addPeer(sessionId, true); this._addPeer(sessionId, true);
}
if (!this.peers[sessionId].ready) {
this.peers[sessionId].connection.once("connect", () => { this.peers[sessionId].connection.once("connect", () => {
this.peers[sessionId].connection.addTrack(track, stream); this.peers[sessionId].connection.addTrack(track, stream);
}); });
@ -190,7 +197,7 @@ class Session extends EventEmitter {
connection.createDataChannel("map", { iceServers: this._iceServers }); connection.createDataChannel("map", { iceServers: this._iceServers });
connection.createDataChannel("token", { iceServers: this._iceServers }); connection.createDataChannel("token", { iceServers: this._iceServers });
} }
const peer = { id, connection, initiator }; const peer = { id, connection, initiator, ready: false };
function sendPeer(id, data, channel) { function sendPeer(id, data, channel) {
peer.connection.send({ id, data }, channel); peer.connection.send({ id, data }, channel);
@ -201,6 +208,9 @@ class Session extends EventEmitter {
} }
function handleConnect() { function handleConnect() {
if (peer.id in this.peers) {
this.peers[peer.id].ready = true;
}
/** /**
* Peer Connect Event - A peer has connected * Peer Connect Event - A peer has connected
* *