Closing the game on Android doesn't properly disconnect the client
Description
On Android, when quitting the app, the server and other players don't get notified. Eventually it will time out due to heartbeats, but that is not ideal. When quitting the game on other platforms, such as Windows, it correctly notifies the server, and the player prefab gets destroyed.
Environment
- OS: Android
- Unity Version: 6000.0.25f1
- Netcode Version: 2.1.1
Additional Context
Specifically tested on:
- Samsung galaxy J5 2017
- Meta quest 2
As far as I know, this is a limitation of mobile operation systems, android will instantly freeze the app when put in the background and will terminate the process nearly instantly as well when the user closes the app. I'm interested if there is a workaround for this tho.
We're also seeing this, which causes players to persist in the lobby indefinitely and blocks them from rejoining for some time.
Meta Quest 3
Would love to have a solution/workaround for this too
Hi,
On mobile (at least I just tested on android) you get some time to execute a bit of code before the application loses focus or is closed. However you need more than Application.wantsToQuit or Application.quitting, you need to hook to MonoBehaviour's OnApplicationFocus for when the app loses focus or OnApplicationPause for when the app is put in background or closed. If the app lost focus or is paused you can call Multiplayer ISession's LeaveAsync to close the session.
If instead you want to leave some grace timeout when the user puts the app in background before killing the session you can use the Lobby's Disconnect Removal Timeout that can be configured in your cloud dashboard/lobby settings.