Hi guys,

I've this simple server code structure. It broadcasts message from a client 
to all connected clients. However, all connections have been cut off when 
someone refresh the page. Anyone can point out what wrong with my code?

var WebSocketServer = require('ws').Server;
var wsServer = new WebSocketServer({ port : 443 }),
clients = [];
wsServer.on('connection', function(ws) {
clients.push(ws);
ws.on('message', function(message, flags) {
for(var i = 0, j = clients.length; i < j; i += 1) {
var xx = clients[i];
}
if (typeof message === 'string') {
var json = JSON.parse(message);
switch(json.type) {
case "msg":
broadcast.data(xx, message);
break;
case "data":
case "demo":
// return;
break;
}
}
});
ws.on('close', function(message) {
var index = clients.indexOf(ws);
if (index !== -1) {
clients.splice(index, 1);
}
});
});
// Broadcast messages to all connected clients
var broadcast = {
data: function(xi, message) {
var that = this;
this.xi = xi;
this.message = message;
this.data.prototype = function() { that.send(this.xi, this.message); }
this.data.prototype(this.xi, this.message);
},
send: function(){
this.xi.send(this.message, {
mask: false
});
}
};


Thank you, 

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/b84c00b5-4cf5-4dbe-bf29-cca1a1451efb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to