stabilize connection

This commit is contained in:
schlagmichdoch 2022-12-31 18:03:37 +01:00
parent 6feeb6d4d9
commit 8dc0f1cb0c
2 changed files with 21 additions and 10 deletions

View file

@ -8,7 +8,7 @@ class ServerConnection {
Events.on('beforeunload', _ => this._disconnect()); Events.on('beforeunload', _ => this._disconnect());
Events.on('pagehide', _ => this._disconnect()); Events.on('pagehide', _ => this._disconnect());
document.addEventListener('visibilitychange', _ => this._onVisibilityChange()); document.addEventListener('visibilitychange', _ => this._onVisibilityChange());
Events.on('online', _ => this._connect()); Events.on('reconnect', _ => this._reconnect());
} }
_connect() { _connect() {
@ -21,7 +21,6 @@ class ServerConnection {
ws.onclose = _ => this._onDisconnect(); ws.onclose = _ => this._onDisconnect();
ws.onerror = e => this._onError(e); ws.onerror = e => this._onError(e);
this._socket = ws; this._socket = ws;
Events.on('reconnect', _ => this._connect());
} }
_onMessage(msg) { _onMessage(msg) {
@ -67,6 +66,8 @@ class ServerConnection {
this.send({ type: 'disconnect' }); this.send({ type: 'disconnect' });
this._socket.onclose = null; this._socket.onclose = null;
this._socket.close(); this._socket.close();
this._socket = null;
Events.fire('disconnect');
} }
_onDisconnect() { _onDisconnect() {
@ -78,10 +79,7 @@ class ServerConnection {
} }
_onVisibilityChange() { _onVisibilityChange() {
if (document.hidden) { if (document.hidden) return;
Events.fire('disconnect');
return;
}
this._connect(); this._connect();
} }
@ -96,6 +94,11 @@ class ServerConnection {
_onError(e) { _onError(e) {
console.error(e); console.error(e);
} }
_reconnect() {
this._disconnect();
this._connect();
}
} }
class Peer { class Peer {
@ -322,6 +325,7 @@ class RTCPeer extends Peer {
console.log('RTC: channel closed', this._peerId); console.log('RTC: channel closed', this._peerId);
Events.fire('peer-disconnected', this._peerId); Events.fire('peer-disconnected', this._peerId);
if (!this._isCaller) return; if (!this._isCaller) return;
if (!this._conn)
this._connect(this._peerId, true); // reopen the channel this._connect(this._peerId, true); // reopen the channel
} }
@ -351,6 +355,7 @@ class RTCPeer extends Peer {
_onError(error) { _onError(error) {
console.error(error); console.error(error);
Events.fire('reconnect');
} }
_send(message) { _send(message) {
@ -388,6 +393,7 @@ class PeersManager {
Events.on('peers', e => this._onPeers(e.detail)); Events.on('peers', e => this._onPeers(e.detail));
Events.on('files-selected', e => this._onFilesSelected(e.detail)); Events.on('files-selected', e => this._onFilesSelected(e.detail));
Events.on('send-text', e => this._onSendText(e.detail)); Events.on('send-text', e => this._onSendText(e.detail));
Events.on('peer-joined', e => this._onPeerJoined(e.detail));
Events.on('peer-left', e => this._onPeerLeft(e.detail)); Events.on('peer-left', e => this._onPeerLeft(e.detail));
Events.on('disconnect', _ => this._clearPeers()); Events.on('disconnect', _ => this._clearPeers());
} }
@ -425,11 +431,15 @@ class PeersManager {
this.peers[message.to].sendText(message.text); this.peers[message.to].sendText(message.text);
} }
_onPeerJoined(peer) {
this._onMessage(peer.id);
}
_onPeerLeft(peerId) { _onPeerLeft(peerId) {
const peer = this.peers[peerId]; const peer = this.peers[peerId];
delete this.peers[peerId]; delete this.peers[peerId];
if (!peer || !peer._peer) return; if (!peer || !peer._conn) return;
peer._peer.close(); peer._conn.close();
} }
_clearPeers() { _clearPeers() {

View file

@ -26,8 +26,7 @@ class PeersUI {
Events.on('peers', e => this._onPeers(e.detail)); Events.on('peers', e => this._onPeers(e.detail));
Events.on('file-progress', e => this._onFileProgress(e.detail)); Events.on('file-progress', e => this._onFileProgress(e.detail));
Events.on('paste', e => this._onPaste(e)); Events.on('paste', e => this._onPaste(e));
Events.on('offline', () => this._clearPeers()); Events.on('disconnect', _ => this._clearPeers());
Events.on('online', () => window.animateBackground(true));
this.peers = {}; this.peers = {};
} }
@ -534,10 +533,12 @@ class NetworkStatusUI {
_showOfflineMessage() { _showOfflineMessage() {
Events.fire('notify-user', 'You are offline'); Events.fire('notify-user', 'You are offline');
window.animateBackground(false);
} }
_showOnlineMessage() { _showOnlineMessage() {
Events.fire('notify-user', 'You are back online'); Events.fire('notify-user', 'You are back online');
window.animateBackground(true);
} }
} }