All WebSocket connections closed when someone refresh a page

41 views
Skip to first unread message

Ket

unread,
Apr 20, 2015, 9:47:28 PM4/20/15
to nod...@googlegroups.com
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, 

Ryan Schmidt

unread,
Apr 21, 2015, 4:53:05 PM4/21/15
to nod...@googlegroups.com

On Apr 20, 2015, at 12:49 PM, Ket wrote:

> However, all connections have been cut off when someone refresh the page.

Isn't that normal browser behavior?

kaustubh....@gmail.com

unread,
Apr 22, 2015, 9:46:55 AM4/22/15
to nod...@googlegroups.com
Client socket is in browser and on reload states of page is discarded and get override.
Reply all
Reply to author
Forward
0 new messages