Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question OnDisconnectClientCallBack problem

Discussion in 'Netcode for GameObjects' started by BlackBoxGaming, Nov 15, 2023.

  1. BlackBoxGaming

    BlackBoxGaming

    Joined:
    Mar 25, 2020
    Posts:
    10
    So, I'm trying to build a PVP multiplayer Android game. This game going to be a 1V1 math solve battle game. Players need to solve 10 math problems in a given time. To Win players need to solve 10 math problems faster than another player.

    I'm using Unity Netcode, Lobby, and Relay to create this game.

    Problem: My game is based on an Android device. So when the client minimizes the game after 10 seconds it fires the NetworkManager.Singleton.OnClientDisconnectCallback += NetworkManager_OnClientDisconnectCallback;

    so in reality client is disconnected but the client says the host disconnected and the host says the client disconnected. Which you can see in the video
    https://drive.google.com/file/d/1PUXheiZnrb3xyBCy1l7omTiNdCDUo9A_/view?usp=sharing

    Again when the host disconnects it says the same thing. Check the video: https://drive.google.com/file/d/1m7iPsE7ftpC5uq-4TH_6_e-xr9E-tApk/view?usp=sharing



    ********PLEASE GIVE A SOLUTION********
     

    Attached Files:

  2. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    244
    This is actually part of both iOS and Android application limitations, when an application is minimized it basically is put into a pause/sleep/hybernation mode.
    You can find more about this topic in this thread.

    Basically, when the application is paused it is no longer updated (i.e. all playerloop updates are halted which means nothing is updated) and the remote host/server will eventually time out the connection. If you want a long term "restore last state" solution, then you will need to use OnApplicationPause to handle storing off state of your session and/or sending some form of message indicating the client is going into a "paused" state so the other client will know what is happening (i.e. show an away icon or the like). If you are using a host as your server, then you will most likely need to devise a way to have the connected client determine when the server has come back online (i.e. potentially some form of lobby meta data state).

    The same OnApplicationPause method is invoked when the application is brought out of its paused state.

    When the pauseStatus parameter is false the application has transitioned from a paused state to an active state.
    When the pauseStatus is true the application is transitioning into a paused/hibernation state.

    When pausing, you will want to save off the session information and so the SessionManagement and Reconnecting Mid-Game documentation will be of interest to you when the game instance transitions from paused to running. Of course, you will need to detect if the session is no longer valid (i.e. the server/host shutsdown while the player has their game instance in a paused state) which you could do by trying to reconnect to the same lobby or relay session (info you would want to persist/save when entering into pause) and if one or both no longer exist then you could present some form of message to the user stating as such.

    Let me know if this information helps?
     
  3. BlackBoxGaming

    BlackBoxGaming

    Joined:
    Mar 25, 2020
    Posts:
    10
    I used OnApplicationPause to call ServerRpc but when the app goes into pause mode the ServerRpc is not calling, when the game resumes the method calls the ServerRpc
    private void OnApplicationPause(bool pause)
    {
    if (pause)
    {
    ManagerMultiplayer.singleton.CallDisconnectServerRpc(ManagerMultiplayer.singleton.My_LocalID);
    Debug.Log("Game Pause");
    }
    else
    {
    Debug.Log("Game Live");
    }
    }
     
  4. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    244
    Hmm...I could have misled you on that statement as it could be that by the time that is triggered updates have stopped for the application...so you send the ServerRpc...it goes into the outbound queue... but since Updates have halted the message queue is not processed (it happens in LateUpdate). You then un-pause the game/app and the message queue is processed...so your queued message is sent.
    :(

    So... one other thing I can think of is to see if by hitting the home button your application loses focus before getting paused. You can use OnApplicationFocus to trap for when the application loses and gains focus (true when focused and false when it loses focus).

    I know when the keyboard comes up it loses focus... so that might not work exactly as you want (not sure if you use the virtual keyboard or if it is accessible while your game has focus) and could be difficult to determine if the player is just using the keyboard or the game/app is about to be paused.

    If that doesn't work, then the only other thing I could think of is to use the current Lobby and see if you could set a value on the player's data via the SDK (messages might still get sent) or use the lobby REST API (you might have to use the UnityWebRequest for this). If you have to do this, then either periodically checking the player's data or upon a disconnect (client or host) checking the player's data to determine if they have gone into a "paused state" could potentially work.
     
    Last edited: Nov 18, 2023