From a4d0d1190b74cd1bfb596b8a02a2deacce3ad86c Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Fri, 28 Feb 2020 02:21:30 -0500 Subject: Fix mobile + Lobby omments --- scripts/sock.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 scripts/sock.js (limited to 'scripts/sock.js') diff --git a/scripts/sock.js b/scripts/sock.js new file mode 100644 index 0000000..78c4195 --- /dev/null +++ b/scripts/sock.js @@ -0,0 +1,52 @@ +function SockWorker(serveraddr, version, callback) { + this.server = serveraddr; + this.version = version; + this.cb = callback; +} + +SockWorker.prototype = { + init: function() { + if(this.server == "" || this.server == null) { + return; + } + try { + this.socket = new WebSocket(this.server); + + this.socket.addEventListener("open", this.o.bind(this)); + this.socket.addEventListener("message", this.msg.bind(this)); + + this.socket.addEventListener("closed", this.c.bind(this)); + this.socket.addEventListener("error", this.err.bind(this)); + } catch (e) { + this.err(); + } + }, + + o: function() { + this.send("version", this.version); + }, + + msg: function(e) { + if(typeof e.data == "string") { + var dat = JSON.parse(e.data) + this.cb(dat); + } + }, + + c: function() { + this.cb({type: "close", data: ""}); + }, + + err: function() { + this.cb({type: "error", data: ""}); + }, + + close: function() { + this.socket.close(); + }, + + send: function(type, data) { + var m = new Message(type, data); + this.socket.send(m.stringify()) + } +}; \ No newline at end of file -- cgit v1.2.3