Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stability] fix socket closing when peer leaves #556

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions client/scripts/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
});
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down