A
A
Abcdefgk2017-09-04 17:11:23
JavaScript
Abcdefgk, 2017-09-04 17:11:23

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);
});

The content of the function is simple, it could be written like this - socket.emit('message', room)
I decided to try out WebRTC, and I definitely need to count the number of clients in the "rooms" on the server, for which there is an object in which the name of the room is a key, and its value is an array with random id of those who joined it . It is assumed that if the client leaves (closes the window), then the name of the room from which he left must be sent in order to subtract an element from the corresponding array.
I did the whole thing at home, and on localhost it all works fine. I decided to try, so to speak, "on the air" - I begged for 14 days of free VPS, uploaded everything there, launched ... ... - but this event does not exist, the message is not sent, the server does not receive anything when the window is closed. The number of people who joined the rooms in the arrays is added, and when leaving, it does not decrease,
I repeat: everything works on localhost. I already wrote this in five variations, and it works in all of them on the localhost, but exactly the same thing in entornets - there is no event, there is no message, the server does not receive anything.
Is there any explanation for this?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
oh, 2017-09-04
well @AnneSmith

https://stackoverflow.com/questions/23399293/nodej...

K
Kiryushka Tsisar, 2017-09-04
@carlcox

window.onbeforeunload = function() {
  return sendMessage(room);
};
?

A
Abcdefgk, 2017-09-05
@Abcdefgk

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);
      }
    }
  }
});

Voila.
True, the misunderstanding remained with the beforeunload event.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question