summaryrefslogtreecommitdiff
path: root/webcards/scripts/socket
diff options
context:
space:
mode:
Diffstat (limited to 'webcards/scripts/socket')
-rw-r--r--webcards/scripts/socket/message.js18
-rw-r--r--webcards/scripts/socket/sock.js48
2 files changed, 36 insertions, 30 deletions
diff --git a/webcards/scripts/socket/message.js b/webcards/scripts/socket/message.js
index 5e821c4..044027d 100644
--- a/webcards/scripts/socket/message.js
+++ b/webcards/scripts/socket/message.js
@@ -1,14 +1,18 @@
-function Message(type, data){
- this.t = type;
- this.d = data;
-}
+'use strict';
+
+class Message{
+ constructor (type, data)
+ {
+ this.t = type;
+ this.d = data;
+ }
-Message.prototype = {
- stringify: function(){
+ stringify ()
+ {
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
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
+}