Answer the question
In order to leave comments, you need to log in
No beforeunload event - any explanation?
Faced such strangeness.
A simple thing on the client, these are the two lines:
window.addEventListener('beforeunload', function() {
sendMessage(room);
});
socket.emit('message', room)
Answer the question
In order to leave comments, you need to log in
window.onbeforeunload = function() {
return sendMessage(room);
};
?
In short, if anyone is interested, then I did everything differently, and it works, sort of.
I came up with a problem for myself when, in order to count clients, I began to write random ID types into arrays of rooms.
Whereas each socket connection has its own unique id - it's their unique id's that need to be added to these arrays. And then I can do without the name of the room the client left.
In the event of an event socket.on('disconnect', ...
(and it is inevitable, like death and taxes), we can easily fix which socket went down - by its very id. It remains only to iterate over the entire storage object and remove this particular element. So like this:
socket.on('disconnect', function() {
for(var key in clientsArray) {
for(var i=0; i<clientsArray[key].length; i++) {
if(clientsArray[key][i] == socket.id) {
clientsArray[key].splice(i, 1);
}
}
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question