Search Unity

Question Any callback from StartClient?

Discussion in 'Netcode for GameObjects' started by Balphagore, Feb 8, 2022.

  1. Balphagore

    Balphagore

    Joined:
    Jul 18, 2019
    Posts:
    82
    Is it possible to know the server status when calling NetworkManager.Singleton.StartClient()? If the server is not active, then no warnings are issued when calling this command, even if you enable developer logs in NetworkManager. Does StartClient have any callback saying that it couldn't connect to the server?
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    666
    Using the Unity Transport you can add a callback on the OnTransportEvent:
    Code (CSharp):
    1. void Start()
    2.     {
    3.         UnityTransport unityTransport = NetworkManager.Singleton.GetComponent<UnityTransport>();
    4.  
    5.         Debug.Log("UnityTransport: " + unityTransport);
    6.  
    7.         unityTransport.OnTransportEvent += OnTransportEvent;
    8.     }
    9.  
    10.     private void OnTransportEvent(Unity.Netcode.NetworkEvent eventType, ulong clientId, ArraySegment<byte> payload, float receiveTime)
    11.     {
    12.         Debug.Log("OnTransportEvent: " + eventType);
    13.     }
    I don't know if you can suppress this line though:
    Code (CSharp):
    1. Debug.LogError("Client failed to connect to server");
     
  3. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    Take a look at the bossroom demo. It has a connection manager you can replicate.
     
  4. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    The callback for connection timeout (e.g. there is no server) is the same as the callback for disconnecting. So if you subscribe to
    NetworkManager.Singleton.OnClientDisconnectCallback
    in your login script, that event will tell you when your connection attempt timed out. You could set a variable like
    wasConnected=true
    upon a successful connection to distinguish between timing out and disconnecting.
     
  5. MiTschMR

    MiTschMR

    Joined:
    Aug 28, 2018
    Posts:
    494
    I am not able to get Unity Transport to work and the `OnClientDisconnectCallback` doesn't fire anything. It doesn't matter from where I subscribe to it, may that be with a custom NetworkManager itself (deriving from it) or a simple Monobehaviour. Honestly, I have no idea what is going on, what I should use, the docs tell you nothing, the boss room is completely outdated etc...

    Edit: Ah, for Unity Transport it needs the com.unity.netcode.adapter.utp package...
     
    Last edited: Feb 18, 2022
  6. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    You should subscribe to it from your login script (e.g. the NetworkBehaviour that also calls StartHost and StartClient), *before* calling these functions (e.g. in OnEnable). Using a Monobehaviour will not work.
     
  7. AaronKB

    AaronKB

    Joined:
    Aug 16, 2019
    Posts:
    27
    I am having trouble and not recieving the client disconnection callbacks when running as a host. The BossRoom example ServerGameGetPortal is a MonoBehaviour and it subscribes to OnClientDisconnectCallback so what is it meant to be?

    I get the client connected callbacks when the host starts but not disconnected callbacks.
     
    ProGameDevUser likes this.