From: Ben Beltran Date: Sat, 20 Jun 2020 12:58:47 +0000 (+0200) Subject: Add retries to socket coordinator X-Git-Url: https://git.r.bdr.sh/rbdr/forum/commitdiff_plain/7372fe18d4fb901d3ebb86e7a73443d4dc3afb70?ds=inline Add retries to socket coordinator --- diff --git a/app/socket_coordinator.js b/app/socket_coordinator.js index d6768ac..f8fb525 100644 --- a/app/socket_coordinator.js +++ b/app/socket_coordinator.js @@ -1,12 +1,52 @@ +import EventEmitter from 'eventemitter3'; import { socketServer } from './config/config'; const internals = { + kReconnectInterval: 3000, // How often we attempt to reconnect + + eventEmitter: new EventEmitter(), // internal event emitter socket: null, // stores the socket connection + retry: null, // stores the retry operation connect() { + console.debug('Connecting socket.'); internals.socket = new WebSocket(socketServer); + + internals.socket.addEventListener('message', internals.onMessage); + internals.socket.addEventListener('error', internals.onError); + internals.socket.addEventListener('close', internals.onClose); + }, + + // Handles socket errors. + + onError(event) { + + console.error('Socket error. Closing connection'); + internals.socket.close(); + }, + + // Handles socket errors. + + onClose(event) { + + console.debug(`Connection closed: ${event.reason || 'Unknown reason'}. Retrying in ${internals.kReconnectInterval}ms`); + + internals.retry && clearTimeout(internals.retry); + internals.retry = setTimeout(() => { + + console.debug('Reconnecting socket.'); + internals.retry = null; + internals.connect(); + }, internals.kReconnectInterval); + }, + + // Forwards events from the socket to our internal event emitter. + + onMessage(event) { + + internals.eventEmitter.emit('message', event); } }; @@ -16,7 +56,7 @@ export const onMessage = function (listener) { internals.connect(); } - internals.socket.addEventListener('message', (message) => { + internals.eventEmitter.on('message', (message) => { listener(JSON.parse(message.data)); });