Search Unity

Can't receive broadcast message, can anyone help?

Discussion in 'Multiplayer' started by gon777, Mar 15, 2017.

  1. gon777

    gon777

    Joined:
    Dec 20, 2016
    Posts:
    7
    Hi,
    I was able to receive broadcast message yesterday but I have no idea how I broke it :(
    My execution sequence is:

    On my game instance -> call MylobbyManager.instance.Starthost(),
    Then on my editor -> call MylobbyManager.instance.ClientStartListening()

    Can anyone help me look at the code and tell me what is wrong?
    Your help will be much appreciated.

    MyLobbyManager
    Code (CSharp):
    1.      
    2. public class MyLobbyManager : NetworkLobbyManager {
    3.     public static MyLobbyManager instance = null;
    4.     public MyNetworkDiscovery myNetworkDiscovery;
    5.  
    6.     public void Start(){
    7.         if (instance == null) {        
    8.             instance = this;
    9.         } else {      
    10.             Destroy (gameObject);
    11.         }
    12.     }
    13.  
    14.     public override void OnStartHost ()
    15.     {
    16.         base.OnStartHost ();
    17.         myNetworkDiscovery.Initialize ();
    18.         myNetworkDiscovery.StartAsServer ();
    19.     }
    20.  
    21.     public void ClientStartListening(){
    22.         myNetworkDiscovery.Initialize ();    
    23.         myNetworkDiscovery.StartAsClient ();
    24.     }
    25.   }
    26. }
    27.  
    mylobbymanager1.png mylobbymanager2.png

    MyNetworkDiscovery
    Code (CSharp):
    1.  
    2. public class MyNetworkDiscovery : NetworkDiscovery {  
    3.     public override void OnReceivedBroadcast (string fromAddress, string data){
    4.         base.OnReceivedBroadcast (fromAddress, data);
    5.         Debug.Log ("Got Message from (" + fromAddress + ") and (" + data + ")");
    6.     }
    7. }
    8.  
    networkdiscovery.png
     
    Last edited: Mar 15, 2017
  2. gon777

    gon777

    Joined:
    Dec 20, 2016
    Posts:
    7
    Turns out you need to call Initilise() and StartAsServer() AFTER calling StartHost(), not in OnStartHost()
     
  3. gon777

    gon777

    Joined:
    Dec 20, 2016
    Posts:
    7
    Also make sure you linked correct NetworkDiscovery reference in editor