Add a disconnect on websocket.

This commit is contained in:
Paul Makles
2021-01-18 11:58:30 +00:00
parent 71f4c3e6b0
commit fbd1e99f32
3 changed files with 13 additions and 4 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "revolt.js",
"version": "3.0.0-beta.10",
"version": "3.0.0-beta.11",
"main": "dist/index.js",
"repository": "https://gitlab.insrt.uk/revolt/revolt.js",
"author": "Paul Makles <insrt.uk>",
+5
View File
@@ -125,6 +125,11 @@ export class Client extends EventEmitter {
/**
* Utility functions.
*/
async logout() {
this.websocket.disconnect();
await Axios.get('/auth/logout');
}
async register(apiURL: string, data: Auth.CreateRequest): Promise<Auth.CreateResponse> {
return (await Axios.post('/auth/create', data, { baseURL: apiURL })).data;
}
+7 -3
View File
@@ -16,6 +16,12 @@ export class WebSocketClient {
this.client = client;
}
disconnect() {
if (typeof this.ws !== 'undefined' && this.ws.readyState === WebSocket.OPEN) {
this.ws.close();
}
}
connect(disallowReconnect?: boolean): Promise<void> {
this.client.emit('connecting');
@@ -28,9 +34,7 @@ export class WebSocketClient {
}
};
if (typeof this.ws !== 'undefined' && this.ws.readyState === WebSocket.OPEN) {
this.ws.close();
}
this.disconnect();
if (typeof this.client.configuration === 'undefined') {
throw new Error("Attempted to open WebSocket without syncing configuration from server.");