From 9548fcf369f4cd44d50e1ffa4b46fc79cd35cc79 Mon Sep 17 00:00:00 2001 From: Fayeed Pawaskar Date: Wed, 7 Aug 2024 20:09:10 +0530 Subject: [PATCH 1/4] chore: Remove unnecessary code in WebSocket connection initialization --- connect/src/core.ts | 2 -- connect/src/web.ts | 27 +++++++++++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/connect/src/core.ts b/connect/src/core.ts index 0304d64f2..b36272e0b 100644 --- a/connect/src/core.ts +++ b/connect/src/core.ts @@ -296,8 +296,6 @@ export default class Ad4mConnect { // Make sure the url is valid try { const websocket = new WebSocket(this.url); - - } catch (e) { this.notifyConnectionChange("not_connected"); return; diff --git a/connect/src/web.ts b/connect/src/web.ts index e33e72f80..e2f8b9dae 100644 --- a/connect/src/web.ts +++ b/connect/src/web.ts @@ -471,10 +471,10 @@ export class Ad4mConnectElement extends LitElement { @state() private _hostingStep = 0; - + @state() private _email = ""; - + @state() private _passowrd = ""; @@ -612,7 +612,7 @@ export class Ad4mConnectElement extends LitElement { private changeEmail(email: string) { this._email = email; - } + } private changePassword(passowrd: string) { this._passowrd = passowrd; @@ -683,10 +683,15 @@ export class Ad4mConnectElement extends LitElement { } private handleConnectionChange(event: ConnectionStates) { - console.log(event); - // this._isOpen = true; + console.log(event); + // this._isOpen = true; if (event === "connected") { - this.changeUIState("requestcap"); + if (this.authState !== "authenticated") { + this.changeUIState("requestcap"); + } else { + this.changeUIState("connected"); + this._isOpen = false; + } } if (event === "disconnected") { this._isOpen = true; @@ -740,6 +745,11 @@ export class Ad4mConnectElement extends LitElement { this._isOpen = true; this.requestUpdate(); const client = await this._client.connect(); + const status = await client.agent.status(); + if (status.isUnlocked && status.isInitialized) { + window.location.reload(); + } + console.log("sssss", status); return client; } @@ -750,6 +760,11 @@ export class Ad4mConnectElement extends LitElement { async connectRemote(url) { try { const client = await this._client.connect(url); + const status = await client.agent.status(); + if (status.isUnlocked && status.isInitialized) { + return client + } + this.changeUIState("requestcap"); return client; } catch (e) { From f4d335c8f2f58a42680bcbbdfa35ba2eb04411c5 Mon Sep 17 00:00:00 2001 From: Fayeed Pawaskar Date: Wed, 7 Aug 2024 20:25:00 +0530 Subject: [PATCH 2/4] fix: Handle WebSocket connection issue with proxy URLs --- connect/src/utils.ts | 2 +- connect/src/web.ts | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/connect/src/utils.ts b/connect/src/utils.ts index 37294c1d4..bcc6c7e6c 100644 --- a/connect/src/utils.ts +++ b/connect/src/utils.ts @@ -14,7 +14,7 @@ export async function connectWebSocket(url, timeout = 10000) { return Promise.race([ new Promise((resolve, reject) => { try { - if (url.includes("proxy")) { + if (!url.includes("localhost")) { resolve(new WebSocket(url)); } const websocket = new WebSocket(url); diff --git a/connect/src/web.ts b/connect/src/web.ts index e2f8b9dae..1649b4dee 100644 --- a/connect/src/web.ts +++ b/connect/src/web.ts @@ -745,11 +745,14 @@ export class Ad4mConnectElement extends LitElement { this._isOpen = true; this.requestUpdate(); const client = await this._client.connect(); - const status = await client.agent.status(); - if (status.isUnlocked && status.isInitialized) { - window.location.reload(); + try { + const status = await client.agent.status(); + if (status.isUnlocked && status.isInitialized) { + window.location.reload(); + } + } catch (e) { + console.warn(e); } - console.log("sssss", status); return client; } From cc116470bc0ed18c5f8043b4a485d53fd9149a08 Mon Sep 17 00:00:00 2001 From: Fayeed Pawaskar Date: Thu, 8 Aug 2024 12:44:40 +0530 Subject: [PATCH 3/4] chore: Update Ad4mConnectElement to handle URL changes and WebSocket connection issues --- connect/src/web.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/connect/src/web.ts b/connect/src/web.ts index 1649b4dee..9ac3fe036 100644 --- a/connect/src/web.ts +++ b/connect/src/web.ts @@ -631,6 +631,11 @@ export class Ad4mConnectElement extends LitElement { } private changeUrl(url) { + if (url !== this._client.url) { + removeForVersion("ad4mtoken") + this._client.setToken(null); + } + this._client.setUrl(url); } @@ -762,6 +767,7 @@ export class Ad4mConnectElement extends LitElement { async connectRemote(url) { try { + this.changeUrl(url); const client = await this._client.connect(url); const status = await client.agent.status(); if (status.isUnlocked && status.isInitialized) { @@ -771,9 +777,8 @@ export class Ad4mConnectElement extends LitElement { this.changeUIState("requestcap"); return client; } catch (e) { - if (e.message === "Socket closed with event 4500 Invalid Compact JWS") { this.changeUIState("requestcap"); - } + this._isOpen = true; } } From d8b6aec992221f48c60cecbd47e6f4b75c2e8d85 Mon Sep 17 00:00:00 2001 From: Fayeed Pawaskar Date: Thu, 8 Aug 2024 14:06:21 +0530 Subject: [PATCH 4/4] chore: Update Ad4mConnectElement to handle URL changes and WebSocket connection issues --- connect/src/core.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connect/src/core.ts b/connect/src/core.ts index b36272e0b..92a61690f 100644 --- a/connect/src/core.ts +++ b/connect/src/core.ts @@ -405,7 +405,7 @@ export default class Ad4mConnect { let token = localStorage.getItem('hosting_token'); const response = await fetch('https://hosting.ad4m.dev/api/service/checkStatus', { - method: 'GET', + method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token