Search Unity

Network discovery broadcast issue android

Discussion in 'Multiplayer' started by Dypiosa, Jan 2, 2019.

  1. Dypiosa

    Dypiosa

    Joined:
    Sep 5, 2018
    Posts:
    4
    Hi!

    Im making a new addition to my PC game (the server) by supporting an Android app (client) that will be a (custom)controller (sort of wifi controller). The server broadcasts it's network address to the client. This is a LAN connection, so only on the same network.

    I have got it to work numerous times, but it is very inconsistent. If I reinstall the APK build of the android app, it works fine, but after 2 or 3 close/open cycles (like really closing the Android app and booting it up again) it stops picking up the broadcast. I have not found a solution for it yet.

    Am I not cleaning old connections or something? I would have thougth that closing the app would clear any NetworkTransport thingies. The only thing I am sure of is that the server broadcast is fine because when I open the client app on another Android device, it works fine while maintaining the same session of the server.

    Also, I am a complete novice in networking so do not blame me :p
    Thanks in advance! :)

    Code of the client-side NetworkDiscovery

    Code (CSharp):
    1.  
    2. public class NetworkDiscoveryController : NetworkDiscovery {
    3.  
    4.     bool hasReceivedBroadcastAtLeastOnce = true;
    5.  
    6.     private void OnEnable()
    7.     {
    8.         if(Application.isEditor){
    9.             Debug.LogWarning("Editor mode, IP: localhost");
    10.             hasReceivedBroadcastAtLeastOnce = false;
    11.             OnReceivedBroadcast("", "localhost:25025");
    12.             return;
    13.         }
    14.  
    15.         Initialize();
    16.         hasReceivedBroadcastAtLeastOnce = false;
    17.         StartAsClient();
    18.     }
    19.  
    20.     public override void OnReceivedBroadcast(string fromAddress, string data)
    21.     {
    22.         try
    23.         {
    24.             if(hasReceivedBroadcastAtLeastOnce){
    25.                 return;
    26.             }
    27.             hasReceivedBroadcastAtLeastOnce = true;
    28.             Debugger.instance.setTextConnected("Broadcast found!");
    29.  
    30.             string[] networkMessages = data.Split(':');
    31.             int port;
    32.             Int32.TryParse(networkMessages.Last(), out port);
    33.             Debug.Log(networkMessages.First());
    34.             Debug.Log(networkMessages.Last());
    35.        
    36.             AndroidInputClient.Instance.networkAddress = networkMessages.First();
    37.             AndroidInputClient.Instance.networkPort = port;
    38.             AndroidInputClient.Instance.client = AndroidInputClient.Instance.StartClient();
    39.  
    40.             gameObject.SetActive(false);
    41.         }
    42.         catch(Exception ex)
    43.         {
    44.             Debug.LogError("OnReceiveBroadcast went badly wrong");
    45.             Debug.LogError(ex.Message);
    46.         }
    47.     }
    48.  
    49.     private void OnApplicationPause(bool pauseStatus)
    50.     {
    51.         if(pauseStatus)
    52.         {
    53.             Debug.LogWarning("Client stopping");
    54.             AndroidInputClient.Instance.StopClient();
    55.             StopBroadcast();
    56.             if(AndroidInputClient.Instance.connectionId != 0){
    57.                 byte error;
    58.                 NetworkTransport.Disconnect(AndroidInputClient.Instance.hostId, AndroidInputClient.Instance.connectionId, out error);
    59.                 Debug.LogError("Disconnect message: " + error);
    60.             }
    61.         }
    62.         else
    63.         {
    64.             OnEnable();
    65.         }
    66.     }
    67. }

    When the client has received the broadcast from the server, I connect the client to the actual server and stop the broadcast.


    Code (CSharp):
    1.  
    2.     public override void OnClientConnect(NetworkConnection nc) {
    3.         base.OnClientConnect(nc);
    4.  
    5.         _networkDiscoveryController.StopBroadcast();
    6.         _networkDiscoveryController.gameObject.SetActive(false);
    7.         connectionId = nc.connectionId;
    8.         hostId = nc.hostId;
    9.         Debug.Log("client! on client connect " + connectionId + " " + hostId);
    10.  
    11.         // Handler setup
    12.         client.RegisterHandler(_stateSyncMsgType, OnServerGameState);
    13.     }
    14.  
    15.     public override void OnClientDisconnect(NetworkConnection nc) {
    16.         StopClient();
    17.         _currentState = States.Launch;
    18.         OnStateChanged(_currentState);
    19.         _networkDiscoveryController.gameObject.SetActive(true);
    20.         Debugger.instance.setTextConnected("Connection lost");
    21.     }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    There's several threads on this issue, as well as on other forums unrelated to Unity or game development. From what I've read it is an android issue, possibly specific to certain devices like Pixel. Sorry my Googlefu is weak right now, or I'd include a link or two.
     
  3. TJD269

    TJD269

    Joined:
    Oct 17, 2017
    Posts:
    19
    Hey man, having the same issue. My software has worked pretty consistently for months, but for some reason one computer on my network rarely will join thru network discovery now. Every other pc joins right on up first try. I've disabled firewalls and everything, and it still is for some reason just this 1 computer. It will usually always work when I restart the pc, but that's not what I would chalk up as reliable. Did you have any luck? @Dypiosa