From 5f9c565df68ee96b7fca7c5db604ad51f8342857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Mon, 12 Apr 2021 21:10:08 +0200 Subject: [PATCH] periodically reconnect WebRTC. --- src/component/main.vue | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/component/main.vue b/src/component/main.vue index eddfb86e..5d6ea5fe 100644 --- a/src/component/main.vue +++ b/src/component/main.vue @@ -463,7 +463,7 @@ this.events.emit('connection.webrtc', 'connected') }) - let webrtcReconnect: any + let webrtcReconnect: any = null this.webrtc.on('disconnected', () => { this.events.emit('connection.webrtc', 'disconnected') this.clearWebRTCState() @@ -478,14 +478,21 @@ } } - // reconnect WebRTC - if (this.connected) { - if (webrtcReconnect) clearTimeout(webrtcReconnect) + // periodically reconnect WebRTC + if (this.connected && !webrtcReconnect) { + webrtcReconnect = setInterval(() => { + // connect only if disconnected + if (this.state.connection.webrtc.status == 'disconnected') { + try { + this.webrtcConnect() + } catch (e) {} + } - webrtcReconnect = setTimeout(() => { - try { - this.webrtcConnect() - } catch (e) {} + // stop reconnecting if connected to webrtc or disconnected from WS + if (this.state.connection.webrtc.status == 'connected' || !this.connected) { + clearInterval(webrtcReconnect) + webrtcReconnect = null + } }, 1000) } })