From 7d22913fd0a5275f647ed8ecdcfaa3ce01d4e581 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Fri, 12 Apr 2019 20:34:45 +0200 Subject: [PATCH] ipc: fix network drops detection and reconnection (#510) --- CHANGELOG.md | 5 ++++ src/client/app/ipc/index.js | 53 ++++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72b846c3..16640fc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 0.45.4 + +- bug fixes + - handle network drops correctly (notify immediately and fix reconnection mechanism) + ## 0.45.3 - bug fixes diff --git a/src/client/app/ipc/index.js b/src/client/app/ipc/index.js index 288e4524..15415bd2 100644 --- a/src/client/app/ipc/index.js +++ b/src/client/app/ipc/index.js @@ -8,9 +8,9 @@ setTimeout(()=>{ locales = require('../locales') }) -var reconnectInterval = 5000, - hearbeatInterval = 25000, - hearbeatTimeout = 5000, +var reconnectTimeout = 1000, + hearbeatInterval = 2000, + hearbeatTimeout = 1000, protocol = document.location.protocol === 'https:' ? 'wss://' : 'ws://' class Ipc extends EventEmitter { @@ -23,13 +23,13 @@ class Ipc extends EventEmitter { this.queue = [] - this.disconnectedOnce = false + this.disconnected = false this.reconnect = undefined this.hearbeat = undefined this.hearbeatTimeout = undefined this.on('pong', ()=>{ - this.hearbeatTimeout = clearTimeout(this.hearbeatTimeout) + clearTimeout(this.hearbeatTimeout) }) this.on('ping', ()=>{ this.socket.send('["pong"]') @@ -61,19 +61,21 @@ class Ipc extends EventEmitter { this.socket = new WebSocket(protocol + window.location.host + '/' + uuid) - this.socket.onopen = (e)=>{ + this.socket.onclose = this.socket.onerror = ()=>{ + if (!this.connected()) this.close() + } - this.reconnect = clearInterval(this.reconnect) + this.socket.onopen = (e)=>{ this.hearbeat = setInterval(()=>{ if (!this.connected()) return this.socket.send('["ping"]') this.hearbeatTimeout = setTimeout(()=>{ - if (this.connected()) this.socket.close() + if (this.connected()) this.close() }, hearbeatTimeout) }, hearbeatInterval) - if (notifications && this.disconnectedOnce) notifications.add({ + if (notifications && this.disconnected) notifications.add({ icon: 'wifi', message: locales('server_connected'), id: 'ipc_state' @@ -82,25 +84,25 @@ class Ipc extends EventEmitter { this.trigger('connect') this.flush() - } + this.socket.onmessage = (e)=>{ + this.receive(e.data) + } - this.socket.onmessage = (e)=>{ - this.receive(e.data) - } - - this.socket.onclose = this.socket.onerror = ()=>{ - if (!this.connected()) this.close() } } close() { + this.socket.onclose = this.socket.onerror = null + + this.socket.close() + this.socket = null - this.hearbeat = clearInterval(this.hearbeat) - this.hearbeatTimeout = clearTimeout(this.hearbeatTimeout) - this.dieTimeout = clearTimeout(this.dieTimeout) + clearInterval(this.hearbeat) + clearTimeout(this.hearbeatTimeout) + notifications.add({ icon: 'wifi', class: 'error', @@ -108,12 +110,13 @@ class Ipc extends EventEmitter { id: 'ipc_state', duration: Infinity }) - this.disconnectedOnce = true - if (!this.reconnect) { - this.reconnect = setInterval(()=>{ - this.open() - }, reconnectInterval) - } + + this.disconnected = true + + clearTimeout(this.reconnect) + this.reconnect = setTimeout(()=>{ + this.open() + }, reconnectTimeout) }