this.emit("ready"); | this.emit("ready"); | ||||
}.bind(this); | }.bind(this); | ||||
this._sock.onclose = function(evt) { | |||||
this.emit("close"); | |||||
}.bind(this); | |||||
this._sock.onmessage = function(evt) { | this._sock.onmessage = function(evt) { | ||||
var obj = JSON.parse(evt.data); | var obj = JSON.parse(evt.data); | ||||
console.log("Received myEvent from server!"); | console.log("Received myEvent from server!"); | ||||
console.log(data); | console.log(data); | ||||
}); | }); | ||||
sock.on("close", function() { | |||||
document.getElementById("message").innerHTML = | |||||
"Connection closed!"; | |||||
}); | |||||
}); | }); | ||||
}); | }); | ||||
</script> | </script> |
function Socket(websock) { | function Socket(websock) { | ||||
this._websock = websock; | this._websock = websock; | ||||
websock.on("close", function() { | |||||
this.emit("close"); | |||||
}.bind(this)); | |||||
websock.on("message", function(msg) { | websock.on("message", function(msg) { | ||||
var obj; | var obj; | ||||
try { | try { | ||||
wss.on("connection", function(websock) { | wss.on("connection", function(websock) { | ||||
var sock = new Socket(websock); | var sock = new Socket(websock); | ||||
this.socks.push(sock); | this.socks.push(sock); | ||||
var i = this.socks.length - 1; | |||||
sock.on("close", function() { | |||||
this.socks.splice(i, 1); | |||||
}.bind(this)); | |||||
this.emit("connection", sock); | this.emit("connection", sock); | ||||
}.bind(this)); | }.bind(this)); |
{ | { | ||||
"name": "socksugar", | "name": "socksugar", | ||||
"version": "0.1.2", | |||||
"version": "0.2.0", | |||||
"description": "Websockets with sugar on top.", | "description": "Websockets with sugar on top.", | ||||
"main": "index.js", | "main": "index.js", | ||||
"scripts": { | "scripts": { |