I am building an online game, and one feature involves being able to put up a game request on the site so that other users can see it, and maybe accept. For this site, I want to remove the game request of a user when they close the page, by either navigating to another page on the site, closing it altogether, following a link, etc. so that other users who are still online don't respond to a dead request.
The issue is, I was planning on writing code to do that and put it in an event listener for the closing of the page, but apparently some of the events have issues with them, according to MDN:
- Unload/befoeunload: This event apparently does not fire reliably on mobile, especially when the user opens another app without closing the browser, and closes the browser with app manager afterwards. I imagine this could lead to problems on the phone, but this is my default option for now.
- Visibility: MDN's suggested alternative to unload, the change in visibility event would fire when a mobile user opens another app, but it also fires when they open another tab in the browser. This would mean they would have to sit there looking at the lobby page until someone accepted their game request, which is not ideal.
- Disconnection: Another thing I was thinking about was listening to the disconnection event fired by a socket (I am using socket.io to manage user connections) but I imagine that a disconnection due to bad network can also fire this event, even if it is momentary. A user would then have to remake their request; also not ideal.
These are my main 3 options for now, and my question is: Is there an event that fires when a page is closed that I can listen to, that works like unload but also accounts for mobile users' issues as mentioned above? Or maybe something like the visibility change, but does not fire when they go to another tab? Or something else entirely that sidesteps both of these issues.
https://stackoverflow.com/questions/66701622/what-is-the-most-reliable-event-to-listen-for-when-window-is-closed March 19, 2021 at 10:08AM
没有评论:
发表评论