diff --git a/client/scripts/network.js b/client/scripts/network.js index e1383f3d..fe246366 100644 --- a/client/scripts/network.js +++ b/client/scripts/network.js @@ -15,9 +15,9 @@ class ServerConnection { if (this._isConnected() || this._isConnecting()) return; const ws = new WebSocket(this._endpoint()); ws.binaryType = 'arraybuffer'; - ws.onopen = e => console.log('WS: server connected'); + ws.onopen = _ => console.log('WS: server connected'); ws.onmessage = e => this._onMessage(e.data); - ws.onclose = e => this._onDisconnect(); + ws.onclose = _ => this._onDisconnect(); ws.onerror = e => console.error(e); this._socket = ws; } @@ -66,13 +66,15 @@ class ServerConnection { this.send({ type: 'disconnect' }); this._socket.onclose = null; this._socket.close(); + Events.fire('disconnect'); } _onDisconnect() { console.log('WS: server disconnected'); Events.fire('notify-user', 'Connection lost. Retry in 5 seconds...'); clearTimeout(this._reconnectTimer); - this._reconnectTimer = setTimeout(_ => this._connect(), 5000); + this._reconnectTimer = setTimeout(this._connect, 5000); + Events.fire('disconnect'); } _onVisibilityChange() { @@ -187,12 +189,12 @@ class Peer { _onChunkReceived(chunk) { if(!chunk.byteLength) return; - + this._digester.unchunk(chunk); const progress = this._digester.progress; this._onDownloadProgress(progress); - // occasionally notify sender about our progress + // occasionally notify sender about our progress if (progress - this._lastProgress < 0.01) return; this._lastProgress = progress; this._sendProgress(progress); @@ -254,7 +256,7 @@ class RTCPeer extends Peer { } _openChannel() { - const channel = this._conn.createDataChannel('data-channel', { + const channel = this._conn.createDataChannel('data-channel', { ordered: true, reliable: true // Obsolete. See https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/reliable }); @@ -296,13 +298,13 @@ class RTCPeer extends Peer { const channel = event.channel || event.target; channel.binaryType = 'arraybuffer'; channel.onmessage = e => this._onMessage(e.data); - channel.onclose = e => this._onChannelClosed(); + channel.onclose = _ => this._onChannelClosed(); this._channel = channel; } _onChannelClosed() { console.log('RTC: channel closed', this._peerId); - if (!this.isCaller) return; + if (!this._isCaller) return; this._connect(this._peerId, true); // reopen the channel } @@ -369,6 +371,7 @@ class PeersManager { Events.on('files-selected', e => this._onFilesSelected(e.detail)); Events.on('send-text', e => this._onSendText(e.detail)); Events.on('peer-left', e => this._onPeerLeft(e.detail)); + Events.on('disconnect', this._clearPeers); } _onMessage(message) { @@ -407,10 +410,16 @@ class PeersManager { _onPeerLeft(peerId) { const peer = this.peers[peerId]; delete this.peers[peerId]; - if (!peer || !peer._peer) return; - peer._peer.close(); + if (!peer || !peer._conn) return; + if (peer._channel) peer._channel.onclose = null; + peer._conn.close(); } + _clearPeers() { + if (this.peers) { + Object.keys(this.peers).forEach(peerId => this._onPeerLeft(peerId)); + } + } } class WSPeer {