switching networks works, established server functions

This commit is contained in:
metacryst
2026-01-16 05:22:52 -06:00
parent d2982543d0
commit d3df5bb6cb
31 changed files with 410 additions and 310 deletions

View File

@@ -1,62 +1,61 @@
class Connection {
connectionTries = 0
connectionTries = 0;
ws;
linkCreated;
wsStatus;
receiveCB;
constructor(receiveCB) {
this.init()
this.receiveCB = receiveCB
this.receiveCB = receiveCB;
}
init() {
if(window.location.hostname.includes("local")) {
this.ws = new WebSocket("ws://" + window.location.host)
} else {
this.ws = new WebSocket("wss://" + window.location.hostname + window.location.pathname)
}
this.ws.addEventListener('open', () => {
this.connectionTries = 0
console.log("Websocket connection established.");
this.ws.addEventListener('message', this.receiveCB)
init = async () => {
return new Promise((resolve, reject) => {
const url = window.location.hostname.includes("local")
? "ws://" + window.location.host
: "wss://" + window.location.hostname + window.location.pathname;
this.ws = new WebSocket(url);
this.ws.addEventListener('open', () => {
this.connectionTries = 0;
console.log("WebSocket connection established.");
this.ws.addEventListener('message', this.receiveCB);
resolve(this.ws); // resolve when open
});
this.ws.addEventListener('close', () => {
console.log('WebSocket closed');
this.checkOpen(); // attempt reconnection
});
this.ws.addEventListener('error', (err) => {
console.error('WebSocket error', err);
reject(err); // reject if error occurs
});
});
this.ws.addEventListener("close", () => {
this.checkOpen();
console.log('Websocket Closed')
})
}
async checkOpen() {
checkOpen = async () => {
if (this.ws.readyState === WebSocket.OPEN) {
return true
return true;
} else {
await this.sleep(this.connectionTries < 20 ? 5000 : 60000)
this.connectionTries++
console.log('Reestablishing connection')
this.init()
await this.sleep(this.connectionTries < 20 ? 5000 : 60000);
this.connectionTries++;
console.log('Reestablishing connection');
await this.init();
}
}
sleep = (time) => {
return new Promise(resolve => {
setTimeout(resolve, time);
});
}
sleep = (time) => new Promise(resolve => setTimeout(resolve, time));
send = (msg) => {
console.log("sending")
if (this.ws.readyState === WebSocket.OPEN) {
this.ws.send(msg);
}
else if(this.connectionTries === 0) {
setTimeout(() => {
this.send(msg)
}, 100)
}
else {
console.error('No websocket connection: Cannot send message');
} else if (this.connectionTries === 0) {
setTimeout(() => this.send(msg), 100);
} else {
console.error('No WebSocket connection: Cannot send message');
}
}
}
export default Connection
export default Connection;

View File

@@ -10,6 +10,10 @@ export default class Socket {
this.connection = new Connection(this.receive);
}
async init() {
await this.connection.init()
}
isOpen() {
if(this.connection.checkOpen()) {
return true;

1
ui/_/code/ws/shim/fs.js Normal file
View File

@@ -0,0 +1 @@
export default {}