diff options
author | Kyle Gunger <corechg@gmail.com> | 2020-02-27 17:48:00 -0500 |
---|---|---|
committer | Kyle Gunger <corechg@gmail.com> | 2020-02-27 17:48:00 -0500 |
commit | ea8f308bc00330b5378eb2b8f16d9cc348719c9a (patch) | |
tree | 34a9074b4687beefd3bf046d68374ef307f178da /webcards/scripts/client/sock.js | |
parent | 62cfd4f23993e04602aa15adf4fc71e6eb48c376 (diff) |
Init WebCards
Diffstat (limited to 'webcards/scripts/client/sock.js')
-rw-r--r-- | webcards/scripts/client/sock.js | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/webcards/scripts/client/sock.js b/webcards/scripts/client/sock.js new file mode 100644 index 0000000..78c4195 --- /dev/null +++ b/webcards/scripts/client/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 |