Answer the question
In order to leave comments, you need to log in
How to delete a connection if the user has moved to another page?
var clients={};
exports.subscribe=function(req,res,id){
if(typeof id=='number'&&id>=0){
if(typeof clients[id]=='undefined')
clients[id]=[];
var cl=clients[id];
}
cl.push(res);
res.on('close',function(){
console.log('disconnect chat №',id);
cl.splice(cl.indexOf(res),1);
});
};
exports.publish=function(message){
var id=message['id'];
clients[id].forEach(function(res){
res.end('{"message":"'+message['body']+'"}');
});
clients[id]=[];
};
res.on('close',function(){...
, if the connection is closed by the client, then it is removed from the array. Answer the question
In order to leave comments, you need to log in
to hang up in window.onbeforeunload the code sending some unsubscribe?
Or double the traffic - expect a mandatory response from the client to each message from the server - at the same time it will help to estimate the speed of communication with this client very roughly.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question