I study on JavaScript, create an online game. I have a problem: it is necessary to remove the player from the room (by closing the tab, by closing the browser, by going to another page); I try to use the beforeunload and unload events, but I can't track it. Share your experience, there may be another way to remove a player from a room?
addEventListener('unload', (event) => {
/**
* Send notify of your absence in room.
*/
$.post("/ajax/player/remove", {
userID: window.user.id,
roomID: window.room.id
});
});