I would like to know how everyone handles Unity Android background behaviour, When the game is minimized the socket does not close at all if he stays in the background for a long time. I tried changing the the duration but it did nothing
_socket = _client.NewSocket(useMainThread: true, new WebSocketAdapter(20));
as a result I manually close the connection whenver the user minimizes the app. and connect again when resumed.
I want this behavior to be linked to the socket timeout. Any tips on that? @sesposito
Unfortunately it’s difficult to give a definitive answer, I believe this changes depending on Android version and other variables (is power saving on, etc). When the app is backgrounded, there’s no guarantee that the connection will remain open, (unless you use specific OS functions to make it so) even if you’re observing that it does for extended periods of time.
I’m not sure what you mean with linking the behavior to the socket timeout.
I’m not experienced in Android, but I think I’d do something like checking for connectivity when the app is foregrounded, and if there is none, I’d reestablish the connection.
Maybe someone in the community has some better input on how to best mange the connections.
Reconnecting when a socket goes inactive is achievable, but a challenge arises on Android devices. If a player minimizes the app, the server doesn’t receive any notification indicating that the player is disconnected if the socket drops from the client side. In multiplayer games, this leaves other players waiting indefinitely without receiving any player-disconnected notification.
While implementing a heartbeat system could address this, it seems excessive for this use case. I’m also curious why the socket timeout setting (in seconds) isn’t triggering on mobile as expected. I know many developers have used Nakama for mobile game development; I’d love to hear how others have handled this issue.
Hey @Sathyaraj
On Mobile, when app goes to background you can stop/kill socket connection client is having. And when app is being restored you can recreate/reconnect session connection.
Now even when you do this, you will have a delay to the server, when server receives that network has been killed, and player has disconnected. Currently what we have noticed delay is between 15-30s could be different on higher latency i expect.
If you don’t kill it if app is in background, you are left on OS to determinate when it will kill/stop app and thus disconnect socket. I wouldn’t advise you to do that. As either in that case server will inventiablly receive that it can not write to the client, at least from what we have observed.
So in our case what we do on mobile:
as soon we receive focus lost, we kill socket.Server will receive that player has disconnected.
Once player is disconnected and match has started, a popup to all still connected players will be issued and internal timer has been started to that disconnected player
Disconnected player in this case has two options on reconnecting:
Full app restart/ or on focus gained to reconnect to the match
Yes, Now OnApplication pause I kill the socket, and on resume I reconnect to it and fetch the latest state. But this process works as expected but not a great user experience. In iOS even pulling down the control center or notification panel causes OnApplicationPause to trigger in Unity.
Not sure how Photon guys are handling the socket in mobile devices. socket what they use respects the background timeout api.