diff options
author | Kyle Gunger <corechg@gmail.com> | 2020-09-15 20:03:52 -0400 |
---|---|---|
committer | Kyle Gunger <corechg@gmail.com> | 2020-09-15 20:03:52 -0400 |
commit | 2ce432034eb35f763182de03fb7b42d2a07afc4b (patch) | |
tree | e57d7bc40d12c32c79f1f16ba669a5426ae80525 /webcards/scripts/socket/sock.js | |
parent | 20201f77b5cf5cbb1c70b1cc51c4108d620a3202 (diff) |
Webcards update from local git server
Diffstat (limited to 'webcards/scripts/socket/sock.js')
-rw-r--r-- | webcards/scripts/socket/sock.js | 48 |
1 files changed, 25 insertions, 23 deletions
diff --git a/webcards/scripts/socket/sock.js b/webcards/scripts/socket/sock.js index cf06a5e..4eacc18 100644 --- a/webcards/scripts/socket/sock.js +++ b/webcards/scripts/socket/sock.js @@ -1,13 +1,15 @@ // A wrapper around the wrapper -function SockWorker(serveraddr, version, callback) { - this.server = serveraddr; - this.version = version; - this.cb = callback; -} +class SockWorker extends EventTarget{ + constructor (serveraddr, version) + { + super(); + + this.server = serveraddr; + this.version = version; + } -SockWorker.prototype = { // Initialize the connection. - init: function() { + init () { if(this.server == "" || this.server == null) { return; } @@ -22,42 +24,42 @@ SockWorker.prototype = { } catch (e) { this.err(); } - }, + } // Called when the connection connects to the server - o: function() { + o () { this.send("version", this.version); - }, + } // Called when the connection gets a message from the server // Attempts to turn the message into a usable object and pass it to the callback - msg: function(e) { + msg (e) { if(typeof e.data == "string") { var dat = JSON.parse(e.data) - this.cb(dat); + this.dispatchEvent(new Event(dat.type, dat.data)); } - }, + } // Called when the connection closes. // Passes a close object to the callback. - c: function() { - this.cb({type: "close", data: ""}); - }, + c () { + this.dispatchEvent(new Event("closed")); + } // Called when the connection encounters an error. // Passes an error to the callback - err: function() { - this.cb({type: "error", data: ""}); - }, + err () { + this.dispatchEvent(new Event("error")); + } // Call to close the connection to the server - close: function() { + close () { this.socket.close(); - }, + } // Send a message to the server - send: function(type, data) { + send (type, data) { var m = new Message(type, data); this.socket.send(m.stringify()) } -};
\ No newline at end of file +} |