From f2a9a33cb0b6ba57fdecd4a21cbf0afdec024ffd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Mon, 8 Feb 2021 17:50:12 +0100 Subject: [PATCH] fix logout and save WS url. --- src/component/internal/websocket.ts | 12 ++++++++++-- src/component/main.vue | 21 ++++++++++++++------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/component/internal/websocket.ts b/src/component/internal/websocket.ts index 7b3a35d7..fdac1ae0 100644 --- a/src/component/internal/websocket.ts +++ b/src/component/internal/websocket.ts @@ -14,11 +14,19 @@ export class NekoWebSocket extends EventEmitter { private _ws?: WebSocket private _timeout?: NodeJS.Timeout private _log: Logger + private _url: string constructor() { super() this._log = new Logger('websocket') + + this._url = '' + this.setUrl(location.href) + } + + public setUrl(url: string) { + this._url = url.replace(/^http/, 'ws').replace(/\/+$/, '') + '/api/ws' } get supported() { @@ -29,14 +37,14 @@ export class NekoWebSocket extends EventEmitter { return typeof this._ws !== 'undefined' && this._ws.readyState === WebSocket.OPEN } - public connect(url: string) { + public connect() { if (this.connected) { throw new Error('attempting to create websocket while connection open') } this.emit('connecting') - this._ws = new WebSocket(url) + this._ws = new WebSocket(this._url) this._log.info(`connecting`) this._ws.onopen = this.onConnected.bind(this) diff --git a/src/component/main.vue b/src/component/main.vue index a7735288..3cdb4ec2 100644 --- a/src/component/main.vue +++ b/src/component/main.vue @@ -153,6 +153,7 @@ public setUrl(url: string) { const httpURL = url.replace(/^ws/, 'http').replace(/\/$|\/ws\/?$/, '') this.api.setUrl(httpURL) + this.websocket.setUrl(httpURL) } public async login(id: string, secret: string) { @@ -162,7 +163,7 @@ await this.api.session.login({ id, secret }) Vue.set(this.state.connection, 'authenticated', true) - this.websocketConnect() + this.websocket.connect() } public async logout() { @@ -170,9 +171,15 @@ throw new Error('client not authenticated') } - this.websocketDisconnect() - await this.api.session.logout() - Vue.set(this.state.connection, 'authenticated', false) + if (this.connected) { + this.websocketDisconnect() + } + + try { + await this.api.session.logout() + } finally { + Vue.set(this.state.connection, 'authenticated', false) + } } public websocketConnect() { @@ -184,7 +191,7 @@ throw new Error('client already connected to websocket') } - this.websocket.connect(this.api.url.replace(/^http/, 'ws') + '/api/ws') + this.websocket.connect() } public websocketDisconnect() { @@ -358,7 +365,7 @@ if (this.authenticated) { setTimeout(() => { try { - this.websocketConnect() + this.websocket.connect() } catch (e) {} }, 1000) } @@ -427,7 +434,7 @@ // check if is user logged in this.api.session.whoami().then(() => { Vue.set(this.state.connection, 'authenticated', true) - this.websocketConnect() + this.websocket.connect() }) // unmute on users first interaction