+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);
}
};
internals.connect();
}
- internals.socket.addEventListener('message', (message) => {
+ internals.eventEmitter.on('message', (message) => {
listener(JSON.parse(message.data));
});