diff options
| author | Kyle Gunger <corechg@gmail.com> | 2020-04-03 16:44:23 -0400 | 
|---|---|---|
| committer | Kyle Gunger <corechg@gmail.com> | 2020-04-03 16:44:23 -0400 | 
| commit | 1697da112a9b9f529fad2f54c62aecd7bbb614e6 (patch) | |
| tree | 68c7cd383107e824953b6aa213d05d9a6842b73f /webcards/scripts/socket | |
| parent | 3040a822085adeb9025ddc1a4573cf37cb37d377 (diff) | |
[WEBCARDS] Update some webcards stuff
Diffstat (limited to 'webcards/scripts/socket')
| -rw-r--r-- | webcards/scripts/socket/message.js | 14 | ||||
| -rw-r--r-- | webcards/scripts/socket/sock.js | 63 | 
2 files changed, 77 insertions, 0 deletions
| diff --git a/webcards/scripts/socket/message.js b/webcards/scripts/socket/message.js new file mode 100644 index 0000000..5e821c4 --- /dev/null +++ b/webcards/scripts/socket/message.js @@ -0,0 +1,14 @@ +function Message(type, data){ +    this.t = type; +    this.d = data; +} + +Message.prototype = { +    stringify: function(){ +        var dat = this.d +        if(typeof dat !== "string"){ +            dat = JSON.stringify(dat); +        } +        return JSON.stringify({type: this.t, data: dat}); +    } +}; diff --git a/webcards/scripts/socket/sock.js b/webcards/scripts/socket/sock.js new file mode 100644 index 0000000..cf06a5e --- /dev/null +++ b/webcards/scripts/socket/sock.js @@ -0,0 +1,63 @@ +// A wrapper around the wrapper  +function SockWorker(serveraddr, version, callback) { +    this.server = serveraddr; +    this.version = version; +    this.cb = callback; +} + +SockWorker.prototype = { +    // Initialize the connection. +    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(); +        } +    }, + +    // Called when the connection connects to the server +    o: function() { +        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) { +        if(typeof e.data == "string") { +            var dat = JSON.parse(e.data) +            this.cb(dat); +        } +    }, + +    // Called when the connection closes. +    // Passes a close object to the callback. +    c: function() { +        this.cb({type: "close", data: ""}); +    }, + +    // Called when the connection encounters an error. +    // Passes an error to the callback +    err: function() { +        this.cb({type: "error", data: ""}); +    }, +     +    // Call to close the connection to the server +    close: function() { +        this.socket.close(); +    }, + +    // Send a message to the server +    send: function(type, data) { +        var m = new Message(type, data); +        this.socket.send(m.stringify()) +    } +};
\ No newline at end of file |