Include connection status on WS client.

This commit is contained in:
Paul Makles
2021-01-27 11:49:44 +00:00
parent 15a0029f46
commit 10f9be875c
2 changed files with 15 additions and 1 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "revolt.js",
"version": "3.0.1-beta.9",
"version": "3.0.1-beta.10",
"main": "dist/index.js",
"repository": "https://gitlab.insrt.uk/revolt/revolt.js",
"author": "Paul Makles <insrt.uk>",
+14
View File
@@ -13,11 +13,20 @@ export class WebSocketClient {
client: Client;
ws?: WebSocket;
connected: boolean;
ready: boolean;
constructor(client: Client) {
this.client = client;
this.connected = false;
this.ready = false;
}
disconnect() {
this.connected = false;
this.ready = false;
if (typeof this.ws !== 'undefined' && this.ws.readyState === WebSocket.OPEN) {
this.ws.close();
}
@@ -70,6 +79,7 @@ export class WebSocketClient {
case 'Authenticated': {
disallowReconnect = false;
this.client.emit('connected');
this.connected = true;
break;
}
case 'Ready': {
@@ -89,6 +99,7 @@ export class WebSocketClient {
}
this.client.emit('ready');
this.ready = true;
resolve();
break;
}
@@ -188,6 +199,9 @@ export class WebSocketClient {
ws.onclose = () => {
this.client.emit('dropped');
this.connected = false;
this.ready = false;
if (!disallowReconnect && this.client.options.autoReconnect) {
backOff(() => this.connect(true)).catch(reject);
}