Skip to content

Commit

Permalink
Merge pull request #512 from coasys/connect-proxy-fixes-2
Browse files Browse the repository at this point in the history
Connect Proxy fixes
  • Loading branch information
lucksus authored Aug 8, 2024
2 parents 8a5328a + d8b6aec commit 8078a3a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
4 changes: 1 addition & 3 deletions connect/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -407,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
Expand Down
2 changes: 1 addition & 1 deletion connect/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
39 changes: 31 additions & 8 deletions connect/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,10 @@ export class Ad4mConnectElement extends LitElement {

@state()
private _hostingStep = 0;

@state()
private _email = "";

@state()
private _passowrd = "";

Expand Down Expand Up @@ -612,7 +612,7 @@ export class Ad4mConnectElement extends LitElement {

private changeEmail(email: string) {
this._email = email;
}
}

private changePassword(passowrd: string) {
this._passowrd = passowrd;
Expand All @@ -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);
}

Expand Down Expand Up @@ -683,10 +688,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;
Expand Down Expand Up @@ -740,6 +750,14 @@ export class Ad4mConnectElement extends LitElement {
this._isOpen = true;
this.requestUpdate();
const client = await this._client.connect();
try {
const status = await client.agent.status();
if (status.isUnlocked && status.isInitialized) {
window.location.reload();
}
} catch (e) {
console.warn(e);
}
return client;
}

Expand All @@ -749,13 +767,18 @@ 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) {
return client
}

this.changeUIState("requestcap");
return client;
} catch (e) {
if (e.message === "Socket closed with event 4500 Invalid Compact JWS") {
this.changeUIState("requestcap");
}
this._isOpen = true;
}
}

Expand Down

0 comments on commit 8078a3a

Please sign in to comment.