This commit is contained in:
Brandan 2023-01-26 19:07:33 -05:00
commit f6f382cdc5
2 changed files with 38 additions and 33 deletions

View file

@ -9,6 +9,7 @@
"author": "Elijah R", "author": "Elijah R",
"license": "GPL-3.0", "license": "GPL-3.0",
"dependencies": { "dependencies": {
"nanoevents": "^7.0.1",
"webpack": "^5.75.0", "webpack": "^5.75.0",
"webpack-cli": "^5.0.1" "webpack-cli": "^5.0.1"
} }

View file

@ -1,6 +1,7 @@
import { guacutils } from "./protocol"; import { guacutils } from "./protocol";
import { config } from "./common"; import { config } from "./common";
import { GetKeysym } from "./keyboard"; import { GetKeysym } from "./keyboard";
import { createNanoEvents } from "nanoevents";
// None = -1 // None = -1
// Has turn = 0 // Has turn = 0
// In queue = <queue position> // In queue = <queue position>
@ -41,6 +42,7 @@ const votetime = document.getElementById("votetime");
const chatListDiv = document.querySelector(".chat-table"); const chatListDiv = document.querySelector(".chat-table");
class CollabVMClient { class CollabVMClient {
eventemitter = createNanoEvents();
socket; socket;
#url; #url;
constructor(url) { constructor(url) {
@ -67,21 +69,14 @@ class CollabVMClient {
if (savedUsername === null) if (savedUsername === null)
this.socket.send(guacutils.encode(["rename"])); this.socket.send(guacutils.encode(["rename"]));
else this.socket.send(guacutils.encode(["rename", savedUsername])); else this.socket.send(guacutils.encode(["rename", savedUsername]));
var f = (e) => { var unbind = this.eventemitter.on('connect', () => {
var msgArr = guacutils.decode(e.data); unbind();
if (msgArr[0] == "connect") { res();
switch (msgArr[1]) { });
case "0": var failunbind = this.eventemitter.on('connectfail', () => {
rej("Failed to connect to the node"); failunbind();
break; rej();
case "1": });
res();
break;
}
this.socket.removeEventListener("message", f);
}
}
this.socket.addEventListener("message", f);
this.socket.send(guacutils.encode(["connect", node])); this.socket.send(guacutils.encode(["connect", node]));
}); });
} }
@ -91,6 +86,16 @@ class CollabVMClient {
case "nop": case "nop":
this.socket.send("3.nop;"); this.socket.send("3.nop;");
break; break;
case "connect":
switch (msgArr[1]) {
case "0":
this.eventemitter.emit('connectfail');
break;
case "1":
this.eventemitter.emit('connect');
break;
}
break;
case "chat": case "chat":
if (!connected) return; if (!connected) return;
for (var i = 1; i < msgArr.length; i += 2) { for (var i = 1; i < msgArr.length; i += 2) {
@ -99,6 +104,19 @@ class CollabVMClient {
chatsound.play(); chatsound.play();
chatListDiv.scrollTop = chatListDiv.scrollHeight; chatListDiv.scrollTop = chatListDiv.scrollHeight;
break; break;
case "list":
var list = [];
for (var i = 1; i < msgArr.length; i+=3) {
list.push({
url: this.#url,
id: msgArr[i],
name: msgArr[i+1],
thumb: msgArr[i+2],
});
}
this.eventemitter.emit('list', list);
break;
case "size": case "size":
if (!connected || msgArr[1] !== "0") return; if (!connected || msgArr[1] !== "0") return;
display.width = msgArr[2]; display.width = msgArr[2];
@ -255,24 +273,10 @@ class CollabVMClient {
} }
async list() { async list() {
return new Promise((res, rej) => { return new Promise((res, rej) => {
var h = (e) => { var unbind = this.eventemitter.on('list', (e) => {
var msgArr = guacutils.decode(e.data); unbind();
if (msgArr[0] === "list") { res(e);
var list = []; })
for (var i = 1; i < msgArr.length; i+=3) {
list.push({
url: this.#url,
id: msgArr[i],
name: msgArr[i+1],
thumb: msgArr[i+2],
});
}
this.socket.removeEventListener("message", h);
res(list);
}
};
this.socket.addEventListener("message", h);
this.socket.send("4.list;"); this.socket.send("4.list;");
}); });
} }