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?
Using the Unity Transport you can add a callback on the OnTransportEvent:
void Start()
{
UnityTransport unityTransport = NetworkManager.Singleton.GetComponent<UnityTransport>();
Debug.Log("UnityTransport: " + unityTransport);
unityTransport.OnTransportEvent += OnTransportEvent;
}
private void OnTransportEvent(Unity.Netcode.NetworkEvent eventType, ulong clientId, ArraySegment<byte> payload, float receiveTime)
{
Debug.Log("OnTransportEvent: " + eventType);
}
I don’t know if you can suppress this line though:
Debug.LogError("Client failed to connect to server");
Take a look at the bossroom demo. It has a connection manager you can replicate.
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.
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…
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.
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.