auto reconnect on connection close
This commit is contained in:
parent
b5d08ed72a
commit
4453f2dc51
1 changed files with 35 additions and 1 deletions
36
src/index.js
36
src/index.js
|
@ -65,12 +65,28 @@ class CollabVMClient {
|
||||||
}
|
}
|
||||||
connect() {
|
connect() {
|
||||||
return new Promise((res, rej) => {
|
return new Promise((res, rej) => {
|
||||||
this.socket = new WebSocket(this.#url, "guacamole");
|
try {
|
||||||
|
this.socket = new WebSocket(this.#url, "guacamole");
|
||||||
|
} catch (e) {
|
||||||
|
rej(e);
|
||||||
|
}
|
||||||
this.socket.addEventListener('message', (e) => this.#onMessage(e));
|
this.socket.addEventListener('message', (e) => this.#onMessage(e));
|
||||||
this.socket.addEventListener('open', () => res(), {once: true});
|
this.socket.addEventListener('open', () => res(), {once: true});
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
#onClose() {
|
||||||
|
cleanup();
|
||||||
|
setTimeout(async () => {
|
||||||
|
try {
|
||||||
|
await this.connect();
|
||||||
|
} catch {
|
||||||
|
console.log("h");
|
||||||
|
this.#onClose();
|
||||||
|
}
|
||||||
|
this.connectToVM(this.node);
|
||||||
|
}, 2000);
|
||||||
|
}
|
||||||
disconnect() {
|
disconnect() {
|
||||||
this.socket.send(guacutils.encode(["disconnect"]));
|
this.socket.send(guacutils.encode(["disconnect"]));
|
||||||
this.socket.close();
|
this.socket.close();
|
||||||
|
@ -80,6 +96,7 @@ class CollabVMClient {
|
||||||
}
|
}
|
||||||
connectToVM(node) {
|
connectToVM(node) {
|
||||||
return new Promise((res, rej) => {
|
return new Promise((res, rej) => {
|
||||||
|
this.socket.addEventListener('close', () => this.#onClose());
|
||||||
this.node = node;
|
this.node = node;
|
||||||
var savedUsername = window.localStorage.getItem("username");
|
var savedUsername = window.localStorage.getItem("username");
|
||||||
if (savedUsername === null)
|
if (savedUsername === null)
|
||||||
|
@ -643,6 +660,23 @@ function screenshotVM() {
|
||||||
}, "image/png");
|
}, "image/png");
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
// Clean everything up after disconnecting
|
||||||
|
function cleanup() {
|
||||||
|
turn = -1;
|
||||||
|
window.username = null;
|
||||||
|
rank = 0;
|
||||||
|
hasTurn = false;
|
||||||
|
if (turninterval) clearInterval(turninterval);
|
||||||
|
if (voteinterval)
|
||||||
|
clearInterval(voteinterval);
|
||||||
|
users.splice(0);
|
||||||
|
userlist.innerHTML = "";
|
||||||
|
Array.prototype.slice.call(staffbtns.children).forEach((curr) => curr.style.display = "none");
|
||||||
|
staffbtns.style.display = "none";
|
||||||
|
usernameSpan.classList = "input-group-text bg-dark text-light";
|
||||||
|
display.height = 0;
|
||||||
|
display.width = 0;
|
||||||
|
}
|
||||||
buttons.screenshot.addEventListener('click', async () => {
|
buttons.screenshot.addEventListener('click', async () => {
|
||||||
var blob = await screenshotVM();
|
var blob = await screenshotVM();
|
||||||
var url = URL.createObjectURL(blob);
|
var url = URL.createObjectURL(blob);
|
||||||
|
|
Loading…
Reference in a new issue