How can I know if a socket disconnection occurred due to the page closing? #5296
              
                Unanswered
              
          
                  
                    
                      trevorwrightdev
                    
                  
                
                  asked this question in
                Q&A
              
            Replies: 1 comment
-
| 
         Hi! I think your best option indeed is the  Alternatively, you could maybe use the  Reference: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon  | 
  
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am in a situation where I want to do different things depending on the following two possibilities:
Right now, by default I keep players logged in to the game for two minutes if they disconnect. This is because unpredictable disconnects can occur. But this means that any player that leaves the game on purpose will linger, which I want to prevent.
The solution I attempted was this:
On the client,
On the server,
This solution works okay on some browsers, but it is unreliable. According to the code for this project, Firefox and Chrome have different behaviors concerning their onbeforeunload disconnect events.
Another solution I tried was to use the disconnect event reason to determine if a socket connection was closed due to the page unloading. The reason for disconnects from the page unload event is "transport close" which according to the documentation can also be a reason for a disconnect if the user just loses connection or switches from wifi to 5G, which is the opposite of what I would have hoped.
If you have any suggestions on how I can tell whether or not a disconnect was due to a page unload, I would greatly appreciate it! Thank you.
Beta Was this translation helpful? Give feedback.
All reactions