Search Unity

UNET disconnects when client hits home button on mobile

Discussion in 'UNet' started by clever, May 28, 2017.

  1. clever

    clever

    Joined:
    Oct 11, 2012
    Posts:
    39
    Hi folks, I looked around the forum for an answer to this and only found the Application.runInBackground solution which is not working for me.

    Basically I have a UNET game where clients connect to an authoritative server. When a client on Android or iOS hits the home button the connection is dropping even if I bring back the game to focus within a second or two... I don't want players to be kicked out just because they accidentally hit the home button

    I set the config.PingTimout to 500ms and the config.DisconnectTimeout to 5000ms, it didn't seem to help, as soon as the game refocuses (even within the 2 second mark) it's already lost connection.

    I'm running 5.6.1

    Any assistance is appreciated! Thanks!
     
  2. donnysobonny

    donnysobonny

    Joined:
    Jan 24, 2013
    Posts:
    220
    I can't quite remember where I read this (been hunting around the documentation with no luck) but Application.runInBackground is only available on standalone builds (ie: windows, mac, linux etc) or platforms that can run the application with priority and in the foreground. On android, your unity application is run as a service, which means it has zero to no ability to run when it is not in focus. This is ultimately the design of android and cannot be worked around.

    So the idea is, if you are building a networked application for android, you need to handle the loss of focus of your game. This doesn't just happen when the "home" button is clicked but can also happen when the status bar is opened, if a system message pops up, if ads are displayed, if videos are viewed etc etc. So ultimately you want to smoothly handle the loss and return of focus by disconnecting/reconnecting the user and handling any cleanup/re-initialization. It's a bit of a pain, but unfortunately you have no choice if you are targeting android.

    Thankfully unity provides some hooks that you can hook into and handle the focus using OnApplicationFocus and OnApplicationPause. From my experience, you need to use a combination of both to get it working correctly. Here's an example of how i do it:

    Code (CSharp):
    1.         void OnApplicationFocus(bool focus) {
    2. #if !UNITY_EDITOR
    3.             Debug.LogFormat("application focus state has changed to {0}", focus);
    4.             if(focus && !this.gameSet) {
    5.                 this.SetUpGame();
    6.             } else if(!focus && this.gameSet) {
    7.                 this.UnsetGame();
    8.             }
    9. #endif
    10.         }
    11.  
    12.         void OnApplicationPause(bool pause) {
    13. #if !UNITY_EDITOR
    14.             Debug.LogFormat("application pause state has changed to {0}", pause);
    15.             if(pause && this.gameSet) {
    16.                 this.UnsetGame();
    17.             } else if(!pause && !this.gameSet) {
    18.                 this.SetUpGame();
    19.             }
    20. #endif
    21.         }
    Copying this, and creating the UnsetGame and SetUpGame methods you can handle the connection/disconnection of the client, and also any cleanup/re-initialization that you need to do.

    Hopefully this helps, good luck!
     
    Oxygeniium likes this.
  3. clever

    clever

    Joined:
    Oct 11, 2012
    Posts:
    39
    Thanks a lot for this! I was hoping there was something an 'easy' fix, since the app pauses fine, it's the connection that drops rather quickly (and with the disconnection the player gets kicked out of play..).

    I'll have to do a speedy reconnect without and delay kicking the player out...

    Thanks again!
     
  4. donnysobonny

    donnysobonny

    Joined:
    Jan 24, 2013
    Posts:
    220
    No problem, glad I could help!