UNET: how to send network data from OnApplicationPause?

Hello everyone,
I’m currently prototyping a mobile game with local multiplayer and I’m using UNET for networking: since the game does not to require Internet access, using UNET with one player acting as host seemed a natural fit.

Playing around with the prototype, one issue I want to address is that the whole networking communication pauses when the game is minimized on the host device (testing on Android); which is understandable, since the OS is pausing the whole Unity process.
The simplest way to address this seems to use the OnApplicationPause method to call a “network pause!” ClientRpc on all clients when the host minimizes the game; however, this is not currently working in my build as the ClientRpc is invoked on clients only after the host resumes the game. If I understood the problem correctly, Unity prepares data for the ClientRpc call on the host but the process is paused before Unity can actually send the data to all clients.
I also tried setting the NetworkManager.singleton.maxDelay field to 0, but it didn’t help.

You can find below the code I added to a NetworkBehaviour:

        float t = 0;
        private void OnApplicationPause(bool pause)
        {
            if (pause && isServer)
            {
                t = NetworkManager.singleton.maxDelay;
                NetworkManager.singleton.maxDelay = 0; //this seems useless :-(
                RpcPause();
            }
        }

        [ClientRpc]
        private void RpcPause()
        {
            Time.timeScale = 0;
            Debug.LogError("Game paused!");
        }

        private void OnApplicationFocus(bool focus)
        {
            if (focus && isServer)
            {
                NetworkManager.singleton.maxDelay = t;
                t = 0;
                RpcResume();
            }
        }

        [ClientRpc]
        private void RpcResume()
        {
            Time.timeScale = 1;
            Debug.LogError("Game resumed!");
        }

I’m currently using HLAPI and a ClientRpc, but any other solution which result in a synchronous data send inside the OnApplicationPause method is fine (network message? call to Transport API?).

Thank you all for your time and help!

Roberto

this.connectionToServer.FlushChannels();

Had a similar problem with sending message. Adding that line after NetworkConnection.Send() call solved this issue for me. I can’t guarantee that this would work on RPC calls.

My first suspicion was that the whole process stops when the screen turns off. Turns out, it doesn’t. The unity process on Android runs in the background like a service(still, it’s not. It’s just a regular old linux process) and exchanges messages while the app is in the background(home key is pressed or screen is off). I captured the packets to come to this conclusion. It seems that the unity process on Android tries to keep the connection, even when it’s the app is in the background with no rendering or callbacks like Update() happening.

Not sure, but my best shot is: the main thread does stop, while the network thread doesn’t. The sockets used for UNet is handled by a separate thread. I think the connection is “preserved” because the network thread responds to the server’s ping messages. RPC calls or Send() calls are not sent because the messages are queued and don’t get the chance to be processed by the main thread until the app is back on again.