From 7980e1a6fcfde00ef97142b8ffc3b90b38067cd1 Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Fri, 6 Jan 2023 15:21:35 +0100 Subject: [PATCH 1/5] fix wrong variable name on connection closing --- client/scripts/network.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/scripts/network.js b/client/scripts/network.js index e1383f3..e274576 100644 --- a/client/scripts/network.js +++ b/client/scripts/network.js @@ -407,8 +407,8 @@ 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; + peer._conn.close(); } } From add1304e70a6c5962327997e4c4e7346f972cb0b Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Fri, 6 Jan 2023 15:22:41 +0100 Subject: [PATCH 2/5] close p2p connection for all devices on disconnect --- client/scripts/network.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/client/scripts/network.js b/client/scripts/network.js index e274576..7108232 100644 --- a/client/scripts/network.js +++ b/client/scripts/network.js @@ -15,10 +15,10 @@ 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.onerror = e => console.error(e); + ws.onclose = _ => this._onDisconnect(); + ws.onerror = e => this._onError(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() { @@ -87,6 +89,11 @@ class ServerConnection { _isConnecting() { return this._socket && this._socket.readyState === this._socket.CONNECTING; } + + _onError(e) { + console.error(e); + this._connect(); + } } class Peer { @@ -369,6 +376,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) { @@ -411,6 +419,11 @@ class PeersManager { peer._conn.close(); } + _clearPeers() { + if (this.peers) { + Object.keys(this.peers).forEach(peerId => this._onPeerLeft(peerId)); + } + } } class WSPeer { From 7b84154e3f9c04e751d895b3e40caf80ea77f7a4 Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Wed, 9 Nov 2022 01:44:17 +0100 Subject: [PATCH 3/5] fix this._isCaller variable typo --- client/scripts/network.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/scripts/network.js b/client/scripts/network.js index 7108232..d4ed0d9 100644 --- a/client/scripts/network.js +++ b/client/scripts/network.js @@ -309,7 +309,7 @@ class RTCPeer extends Peer { _onChannelClosed() { console.log('RTC: channel closed', this._peerId); - if (!this.isCaller) return; + if (!this._isCaller) return; this._connect(this._peerId, true); // reopen the channel } From 8b8c887458d5a87e22450860695797fa5f2f45d5 Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Fri, 6 Jan 2023 15:49:30 +0100 Subject: [PATCH 4/5] only reopen connection if it exists --- client/scripts/network.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client/scripts/network.js b/client/scripts/network.js index d4ed0d9..2e5a59a 100644 --- a/client/scripts/network.js +++ b/client/scripts/network.js @@ -194,12 +194,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); @@ -261,7 +261,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 }); @@ -309,7 +309,7 @@ class RTCPeer extends Peer { _onChannelClosed() { console.log('RTC: channel closed', this._peerId); - if (!this._isCaller) return; + if (!this._isCaller || !this._conn) return; this._connect(this._peerId, true); // reopen the channel } From d99926f657c228c0aad3d865ff39d47d29bf4758 Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Fri, 6 Jan 2023 16:05:17 +0100 Subject: [PATCH 5/5] fix reconnection _onChannelClosed() --- client/scripts/network.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/client/scripts/network.js b/client/scripts/network.js index 2e5a59a..a14200e 100644 --- a/client/scripts/network.js +++ b/client/scripts/network.js @@ -303,13 +303,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 || !this._conn) return; + if (!this._isCaller) return; this._connect(this._peerId, true); // reopen the channel } @@ -416,6 +416,7 @@ class PeersManager { const peer = this.peers[peerId]; delete this.peers[peerId]; if (!peer || !peer._conn) return; + peer._channel.onclose = null; peer._conn.close(); }