From b2ba5088542c4fb1ea85b3432efbf8f4990aa0dc Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Fri, 9 Feb 2018 10:56:01 +0100 Subject: [PATCH] ipc: clean and use same method to check connection state on both sides --- app/main/ipc/client.js | 3 +- resources/docs/editing-sessions.md | 2 +- src/browser/js/app/ipc/index.js | 47 ++++++++++++++---------------- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/app/main/ipc/client.js b/app/main/ipc/client.js index fb31abec..7adc0e89 100644 --- a/app/main/ipc/client.js +++ b/app/main/ipc/client.js @@ -47,7 +47,7 @@ class Socket extends EventEmitter { this.hearbeat = setInterval(()=>{ this.socket.send(`["ping"]`) this.hearbeatTimeout = setTimeout(()=>{ - this.socket.close() + if (this.connected()) this.socket.close() }, hearbeatTimeout) }, hearbeatInterval) @@ -56,6 +56,7 @@ class Socket extends EventEmitter { close() { this.hearbeat = clearInterval(this.hearbeat) + this.hearbeatTimeout = clearTimeout(this.hearbeatTimeout) this.dieTimeout = setTimeout(()=>{ diff --git a/resources/docs/editing-sessions.md b/resources/docs/editing-sessions.md index 0e0b07d5..69c2171d 100644 --- a/resources/docs/editing-sessions.md +++ b/resources/docs/editing-sessions.md @@ -1,6 +1,6 @@ # Editing sessions -The session editor can be enabled from the sidepanel. Once enabled, clicking on a widget will make it the *selected widget*, adding a fancy dashed blue border to it. +The session editor can be enabled from the sidepanel. Once enabled, clicking on a widget will make it the *selected widget*. ## Root panel diff --git a/src/browser/js/app/ipc/index.js b/src/browser/js/app/ipc/index.js index 6248edf7..56d5af39 100644 --- a/src/browser/js/app/ipc/index.js +++ b/src/browser/js/app/ipc/index.js @@ -12,7 +12,6 @@ class Ipc extends EventEmitter { super() this.socket = null - this.connected = false this.queue = [] @@ -44,37 +43,18 @@ class Ipc extends EventEmitter { } - close() { - - this.socket = null - - this.connected = false - - this.hearbeat = clearInterval(this.hearbeat) - this.hearbeatTimeout = clearTimeout(this.hearbeatTimeout) - - if (!this.reconnect) { - this.reconnect = setInterval(()=>{ - this.open() - }, reconnectInterval) - } - - } - open() { this.socket = new WebSocket('ws://' + window.location.host + '/' + uuid) this.socket.onopen = (e)=>{ - this.connected = true - this.reconnect = clearInterval(this.reconnect) this.hearbeat = setInterval(()=>{ this.socket.send(`["ping"]`) this.hearbeatTimeout = setTimeout(()=>{ - this.socket.close() + if (this.connected()) this.socket.close() }, hearbeatTimeout) }, hearbeatInterval) @@ -87,16 +67,33 @@ class Ipc extends EventEmitter { this.receive(e.data) } - this.socket.onclose = (e)=>{ + this.socket.onclose = this.socket.onerror = ()=>{ this.close() } - this.socket.onerror = (e)=>{ - this.close() + } + + close() { + + this.socket = null + + this.hearbeat = clearInterval(this.hearbeat) + this.hearbeatTimeout = clearTimeout(this.hearbeatTimeout) + + if (!this.reconnect) { + this.reconnect = setInterval(()=>{ + this.open() + }, reconnectInterval) } } + connected() { + + return this.socket.readyState == this.socket.OPEN + + } + receive(message) { if (typeof message == 'string') { @@ -117,7 +114,7 @@ class Ipc extends EventEmitter { var packet = JSON.stringify([event, data]) - if (this.connected) { + if (this.connected()) { this.socket.send(packet) } else { this.queue.push(packet)