From 968ede6f58969b07e1a28f665034b1b319dd2868 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Mon, 24 May 2021 22:50:16 +0200 Subject: [PATCH] Sessions working --- www/res/js/modules/usimp.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/www/res/js/modules/usimp.js b/www/res/js/modules/usimp.js index 016dda8..d826f5d 100644 --- a/www/res/js/modules/usimp.js +++ b/www/res/js/modules/usimp.js @@ -106,7 +106,7 @@ export class Session { token; httpBaseUrl; - wsConnection; + websocket; numRequests; constructor(domain) { @@ -122,8 +122,8 @@ export class Session { const protocols = this.server.protocols; /* - if ("wss" in protocols) { - this.wsConnection = new WebSocket(`wss://${host}:${protocols.wss}/_usimp/websocket`, ["usimp"]); + if ("ws" in protocols) { + this.websocket = new WebSocket(`ws://${host}:${protocols.ws}/_usimp/websocket`, ["usimp"]); } */ @@ -147,8 +147,8 @@ export class Session { async send(endpoint, data, timeout = 2000, forceHttp = false) { this.numRequests++; - if (!forceHttp && this.wsConnection) { - this.wsConnection.send(JSON.stringify({ + if (!forceHttp && this.websocket) { + this.websocket.send(JSON.stringify({ 'request_num': this.numRequests, 'data': data })); @@ -159,7 +159,6 @@ export class Session { let headers = { 'Content-Type': 'application/json', 'To-Domain': this.domain.id, - 'From-Domain': this.domain.id, }; if (this.token) { headers['Authorization'] = `usimp ${this.token}`; @@ -186,7 +185,7 @@ export class Session { const resHttp = await this.send("ping", {}, undefined, true); result.http = resHttp.duration; - if (this.wsConnection) { + if (this.websocket) { const resWs = await this.send("ping", {}); result.ws = resWs.duration; } @@ -215,13 +214,12 @@ export class Session { subscribe(func) { this.numRequests++; - if (this.wsConnection) { + if (this.websocket) { // TODO } else { let headers = { 'Content-Type': 'application/json', 'To-Domain': this.domain.id, - 'From-Domain': this.domain.id, }; if (this.token) { headers['Authorization'] = `usimp ${this.token}`; @@ -234,8 +232,14 @@ export class Session { }).then(response => { return response.json(); }).then(response => { - this.subscribe(func); - func(response); + if (response.status === "success") { + this.subscribe(func); + func(response); + } else { + setTimeout(() => { + this.subscribe(func); + }, 1000); + } }).catch(() => { setTimeout(() => { this.subscribe(func);