summaryrefslogtreecommitdiff
path: root/scripts/socket/sock.js
diff options
context:
space:
mode:
authorKyle Gunger <kgunger12@gmail.com>2022-07-24 23:00:59 -0400
committerKyle Gunger <kgunger12@gmail.com>2022-07-24 23:00:59 -0400
commit7abeb4a35a0105b94e599de9970404c98790b4d9 (patch)
tree0cbd81329df21fcdbc5a896a0711657fc60958a1 /scripts/socket/sock.js
parent50a1c6f8432b541d84a1326ae8515589440a3804 (diff)
Clean up game element
Diffstat (limited to 'scripts/socket/sock.js')
-rw-r--r--scripts/socket/sock.js17
1 files changed, 16 insertions, 1 deletions
diff --git a/scripts/socket/sock.js b/scripts/socket/sock.js
index 2135551..4c64e02 100644
--- a/scripts/socket/sock.js
+++ b/scripts/socket/sock.js
@@ -1,16 +1,22 @@
'use strict';
+
+const VALID_EVENTS = ["error", "closed", "handshake", "menu", "game", "chat", "ping"];
+
// A wrapper around the wrapper
class SockWorker extends EventTarget{
+
constructor (serveraddr, version)
{
super();
this.server = serveraddr;
this.version = version;
+ this.handshake = false;
}
// Initialize the connection.
init () {
+ this.handshake = false;
if(this.server == "" || this.server == null) {
return;
}
@@ -37,7 +43,16 @@ class SockWorker extends EventTarget{
msg (e) {
if(typeof e.data == "string") {
let dat = JSON.parse(e.data);
- this.dispatchEvent(new CustomEvent(dat.type, {detail: dat.data}));
+
+ if (!this.handshake) {
+ if (dat.type == "ready")
+ this.handshake = true;
+ this.dispatchEvent(new CustomEvent("handshake", {detail: dat.type}));
+ return;
+ }
+
+ if (VALID_EVENTS.includes(dat.type))
+ this.dispatchEvent(new CustomEvent(dat.type, {detail: dat.data}));
}
}