From 7abeb4a35a0105b94e599de9970404c98790b4d9 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Sun, 24 Jul 2022 23:00:59 -0400 Subject: Clean up game element --- scripts/socket/sock.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'scripts/socket') 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})); } } -- cgit v1.2.3