From 3601ace829608bfe59a8490841c7c5284acc6ac8 Mon Sep 17 00:00:00 2001 From: RobinLinus Date: Fri, 21 Sep 2018 20:59:34 +0200 Subject: [PATCH] Reconnect on rejoin room --- client/scripts/network.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/client/scripts/network.js b/client/scripts/network.js index c986092..bf30fe5 100644 --- a/client/scripts/network.js +++ b/client/scripts/network.js @@ -4,6 +4,8 @@ class ServerConnection { this._connect(); Events.on('beforeunload', e => this._disconnect(), false); Events.on('pagehide', e => this._disconnect(), false); + document.addEventListener('visibilitychange', e => this._onVisibilityChange()); + } _connect() { @@ -71,6 +73,11 @@ class ServerConnection { clearTimeout(this._reconnectTimer); this._reconnectTimer = setTimeout(_ => this._connect(), 5000); } + + _onVisibilityChange() { + if (document.hidden) return; + this._connect(); + } } class Peer { @@ -296,8 +303,13 @@ class RTCPeer extends Peer { _onConnectionStateChange(e) { console.log('RTC: state changed:', this._peer.connectionState); switch (this._peer.connectionState) { - case 'disconnected': this._onChannelClosed(); - break; + case 'disconnected': + this._onChannelClosed(); + break; + case 'failed': + this._peer = null; + this._onChannelClosed(); + break; } } @@ -312,7 +324,7 @@ class RTCPeer extends Peer { refresh() { // check if channel open. otherwise create one if (this._peer && this._channel && this._channel.readyState !== 'open') return; - this._createChannel(this._peerId, this._isCaller); + this._start(this._peerId, this._isCaller); } }