Skip to content
Snippets Groups Projects
Commit 96d14d42 authored by Tobias Ullerich's avatar Tobias Ullerich
Browse files

#187 - Add reconnect on connection close

parent f23253f9
No related branches found
No related tags found
No related merge requests found
...@@ -12,7 +12,7 @@ const uuid = require('uuid'); ...@@ -12,7 +12,7 @@ const uuid = require('uuid');
class ModuleInstance extends InstanceBase { class ModuleInstance extends InstanceBase {
isInitialized = false isInitialized = false
// language=RegExp // language=RegExp
wsRegex = '^wss?:\\/\\/([\\da-z\\.-]+)(:\\d{1,5})?(?:\\/(.*))?$' wsRegex = '^wss?:\\/\\/([\\da-z.-]+)(:\\d{1,5})?(?:\\/(.*))?$'
messageHandlers = { messageHandlers = {
'project-current': new ProjectUpdate(), 'project-current': new ProjectUpdate(),
...@@ -39,7 +39,10 @@ class ModuleInstance extends InstanceBase { ...@@ -39,7 +39,10 @@ class ModuleInstance extends InstanceBase {
// When module gets deleted // When module gets deleted
async destroy() { async destroy() {
this.isInitialized = false this.isInitialized = false
if (this.reconnect_timer) {
clearTimeout(this.reconnect_timer)
this.reconnect_timer = null
}
if (this.ws) { if (this.ws) {
this.ws.close(1000) this.ws.close(1000)
delete this.ws delete this.ws
...@@ -75,6 +78,14 @@ class ModuleInstance extends InstanceBase { ...@@ -75,6 +78,14 @@ class ModuleInstance extends InstanceBase {
tooltip: 'Log incomming and outcomming messages', tooltip: 'Log incomming and outcomming messages',
width: 6, width: 6,
}, },
{
type: 'checkbox',
id: 'reconnect',
label: 'Reconnect',
tooltip: 'Reconnect on WebSocket error (after 5 secs)',
width: 6,
default: true,
},
] ]
} }
...@@ -88,6 +99,17 @@ class ModuleInstance extends InstanceBase { ...@@ -88,6 +99,17 @@ class ModuleInstance extends InstanceBase {
// Websocket handling // Websocket handling
maybeReconnect() {
if (this.isInitialized && this.config.reconnect) {
if (this.reconnect_timer) {
clearTimeout(this.reconnect_timer)
}
this.reconnect_timer = setTimeout(() => {
this.initWebSocket()
}, 1000)
}
}
initWebSocket() { initWebSocket() {
if (this.reconnect_timer) { if (this.reconnect_timer) {
clearTimeout(this.reconnect_timer) clearTimeout(this.reconnect_timer)
...@@ -123,6 +145,7 @@ class ModuleInstance extends InstanceBase { ...@@ -123,6 +145,7 @@ class ModuleInstance extends InstanceBase {
this.ws.on('close', (code) => { this.ws.on('close', (code) => {
this.log('debug', `Connection closed with code ${code}`) this.log('debug', `Connection closed with code ${code}`)
this.updateStatus(InstanceStatus.Disconnected, `Connection closed with code ${code}`) this.updateStatus(InstanceStatus.Disconnected, `Connection closed with code ${code}`)
this.maybeReconnect();
}) })
this.ws.on('message', this.messageReceivedFromWebSocket.bind(this)) this.ws.on('message', this.messageReceivedFromWebSocket.bind(this))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment