2019-06-29 23:57:11 +00:00
|
|
|
import Sockette from 'sockette';
|
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
|
|
|
|
export const SOCKET_EVENTS = [
|
|
|
|
'SOCKET_OPEN',
|
|
|
|
'SOCKET_RECONNECT',
|
|
|
|
'SOCKET_CLOSE',
|
|
|
|
'SOCKET_ERROR',
|
|
|
|
];
|
|
|
|
|
|
|
|
export class Websocket extends EventEmitter {
|
2020-04-17 19:41:47 +00:00
|
|
|
// Timer instance for this socket.
|
|
|
|
private timer: any = null;
|
|
|
|
|
|
|
|
// The backoff for the timer, in milliseconds.
|
|
|
|
private backoff = 5000;
|
|
|
|
|
2019-09-25 03:20:29 +00:00
|
|
|
// The socket instance being tracked.
|
|
|
|
private socket: Sockette | null = null;
|
|
|
|
|
|
|
|
// The URL being connected to for the socket.
|
|
|
|
private url: string | null = null;
|
|
|
|
|
|
|
|
// The authentication token passed along with every request to the Daemon.
|
|
|
|
// By default this token expires every 15 minutes and must therefore be
|
|
|
|
// refreshed at a pretty continuous interval. The socket server will respond
|
|
|
|
// with "token expiring" and "token expired" events when approaching 3 minutes
|
|
|
|
// and 0 minutes to expiry.
|
2020-04-07 04:03:00 +00:00
|
|
|
private token = '';
|
2019-09-25 03:20:29 +00:00
|
|
|
|
|
|
|
// Connects to the websocket instance and sets the token for the initial request.
|
2019-12-22 01:31:04 +00:00
|
|
|
connect (url: string): this {
|
2019-09-25 03:20:29 +00:00
|
|
|
this.url = url;
|
2020-04-17 19:41:47 +00:00
|
|
|
|
2019-12-22 01:31:04 +00:00
|
|
|
this.socket = new Sockette(`${this.url}`, {
|
2019-09-25 03:20:29 +00:00
|
|
|
onmessage: e => {
|
|
|
|
try {
|
2020-04-07 04:03:00 +00:00
|
|
|
const { event, args } = JSON.parse(e.data);
|
2019-09-25 05:05:48 +00:00
|
|
|
args ? this.emit(event, ...args) : this.emit(event);
|
2019-09-25 03:20:29 +00:00
|
|
|
} catch (ex) {
|
|
|
|
console.warn('Failed to parse incoming websocket message.', ex);
|
|
|
|
}
|
|
|
|
},
|
2019-12-22 01:31:04 +00:00
|
|
|
onopen: () => {
|
2020-04-17 19:41:47 +00:00
|
|
|
// Clear the timers, we managed to connect just fine.
|
|
|
|
this.timer && clearTimeout(this.timer);
|
|
|
|
this.backoff = 5000;
|
|
|
|
|
2019-12-22 01:31:04 +00:00
|
|
|
this.emit('SOCKET_OPEN');
|
|
|
|
this.authenticate();
|
|
|
|
},
|
|
|
|
onreconnect: () => {
|
|
|
|
this.emit('SOCKET_RECONNECT');
|
|
|
|
this.authenticate();
|
|
|
|
},
|
2019-09-25 03:20:29 +00:00
|
|
|
onclose: () => this.emit('SOCKET_CLOSE'),
|
2020-04-17 19:41:47 +00:00
|
|
|
onerror: error => this.emit('SOCKET_ERROR', error),
|
2019-09-25 03:20:29 +00:00
|
|
|
});
|
2019-12-22 01:31:04 +00:00
|
|
|
|
2020-04-17 19:41:47 +00:00
|
|
|
this.timer = setTimeout(() => {
|
|
|
|
this.backoff = (this.backoff + 2500 >= 20000) ? 20000 : this.backoff + 2500;
|
|
|
|
this.socket && this.socket.close();
|
|
|
|
clearTimeout(this.timer);
|
|
|
|
|
|
|
|
// Re-attempt connecting to the socket.
|
|
|
|
this.connect(url);
|
|
|
|
}, this.backoff);
|
2019-09-25 03:20:29 +00:00
|
|
|
|
2020-04-17 19:41:47 +00:00
|
|
|
return this;
|
2019-09-25 03:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the authentication token to use when sending commands back and forth
|
|
|
|
// between the websocket instance.
|
2019-12-22 01:31:04 +00:00
|
|
|
setToken (token: string, isUpdate = false): this {
|
2019-09-25 03:20:29 +00:00
|
|
|
this.token = token;
|
|
|
|
|
2019-12-22 01:31:04 +00:00
|
|
|
if (isUpdate) {
|
|
|
|
this.authenticate();
|
2019-09-28 20:09:47 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 03:20:29 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-12-22 01:31:04 +00:00
|
|
|
authenticate () {
|
|
|
|
if (this.url && this.token) {
|
|
|
|
this.send('auth', this.token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-29 23:57:11 +00:00
|
|
|
close (code?: number, reason?: string) {
|
2019-09-28 20:09:47 +00:00
|
|
|
this.url = null;
|
|
|
|
this.token = '';
|
2019-09-09 00:48:37 +00:00
|
|
|
this.socket && this.socket.close(code, reason);
|
2019-06-29 23:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
open () {
|
2019-09-09 00:48:37 +00:00
|
|
|
this.socket && this.socket.open();
|
2019-06-29 23:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reconnect () {
|
2019-09-09 00:48:37 +00:00
|
|
|
this.socket && this.socket.reconnect();
|
2019-06-29 23:57:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 00:18:17 +00:00
|
|
|
send (event: string, payload?: string | string[]) {
|
2019-09-09 00:48:37 +00:00
|
|
|
this.socket && this.socket.send(JSON.stringify({
|
2019-09-25 03:20:29 +00:00
|
|
|
event,
|
|
|
|
args: Array.isArray(payload) ? payload : [ payload ],
|
2019-06-30 00:18:17 +00:00
|
|
|
}));
|
2019-06-29 23:57:11 +00:00
|
|
|
}
|
|
|
|
}
|