Skip to content

Commit dbd1273

Browse files
committed
Fix socket closing when peer leaves (snapdrop pr SnapDrop#556)
1 parent 219df9d commit dbd1273

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

client/scripts/network.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class ServerConnection {
1616

1717
const ws = new WebSocket(this._endpoint() + `?isTerminal=${Number(isTerminal)}`);
1818
ws.binaryType = 'arraybuffer';
19-
ws.onopen = e => console.log('WS: server connected');
19+
ws.onopen = _ => console.log('WS: server connected');
2020
ws.onmessage = e => this._onMessage(e.data);
21-
ws.onclose = e => this._onDisconnect();
21+
ws.onclose = _ => this._onDisconnect();
2222
ws.onerror = e => console.error(e);
2323
this._socket = ws;
2424
}
@@ -67,13 +67,15 @@ class ServerConnection {
6767
this.send({ type: 'disconnect' });
6868
this._socket.onclose = null;
6969
this._socket.close();
70+
Events.fire('disconnect');
7071
}
7172

7273
_onDisconnect() {
7374
console.log('WS: server disconnected');
7475
Events.fire('notify-user', 'Connessione persa, riprovo tra poco');
7576
clearTimeout(this._reconnectTimer);
76-
this._reconnectTimer = setTimeout(_ => this._connect(location.hash.startsWith('#terminal')), 5000);
77+
this._reconnectTimer = setTimeout(this._connect, 5000);
78+
Events.fire('disconnect');
7779
}
7880

7981
_onVisibilityChange() {
@@ -255,7 +257,7 @@ class RTCPeer extends Peer {
255257
}
256258

257259
_openChannel() {
258-
const channel = this._conn.createDataChannel('data-channel', {
260+
const channel = this._conn.createDataChannel('data-channel', {
259261
ordered: true,
260262
reliable: true // Obsolete. See https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/reliable
261263
});
@@ -297,7 +299,7 @@ class RTCPeer extends Peer {
297299
const channel = event.channel || event.target;
298300
channel.binaryType = 'arraybuffer';
299301
channel.onmessage = e => this._onMessage(e.data);
300-
channel.onclose = e => this._onChannelClosed();
302+
channel.onclose = _ => this._onChannelClosed();
301303
this._channel = channel;
302304
}
303305

@@ -370,6 +372,7 @@ class PeersManager {
370372
Events.on('files-selected', e => this._onFilesSelected(e.detail));
371373
Events.on('send-text', e => this._onSendText(e.detail));
372374
Events.on('peer-left', e => this._onPeerLeft(e.detail));
375+
Events.on('disconnect', this._clearPeers);
373376
}
374377

375378
_onMessage(message) {
@@ -393,6 +396,12 @@ class PeersManager {
393396
})
394397
}
395398

399+
_clearPeers() {
400+
if (this.peers) {
401+
Object.keys(this.peers).forEach(peerId => this._onPeerLeft(peerId));
402+
}
403+
}
404+
396405
sendTo(peerId, message) {
397406
this.peers[peerId].send(message);
398407
}
@@ -408,8 +417,9 @@ class PeersManager {
408417
_onPeerLeft(peerId) {
409418
const peer = this.peers[peerId];
410419
delete this.peers[peerId];
411-
if (!peer || !peer._peer) return;
412-
peer._peer.close();
420+
if (!peer || !peer._conn) return;
421+
if (peer._channel) peer._channel.onclose = null;
422+
peer._conn.close();
413423
}
414424

415425
}

0 commit comments

Comments
 (0)