Commit 7372fe18 authored by Ben Beltran's avatar Ben Beltran

Add retries to socket coordinator

parent 8050f772
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));
});
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment