How to expect for errors when starting host/client in Netcode for Gameobjects

I’m working with Netcode for Game Objects in Unity 6.0 using UnityTransport and FacepunchTransport (separately) How can I do something like that?

try
{
    NetworkManager.Singleton.StartHost(); // Port already binded error
    // NetworkManager.Singleton.StartClient(); // Connection Timeout
}
catch (System.Exception e)
{
    OutputScreen.Instance.ShowScreen(e.Message, "Return", null);
}

If something goes wrong, i have no way to tell the user what happened, just a generic error message. I want to be more in control about errors and stuff like that. Do I need to acess a event on the tranposer itself to get errors like these?

I don’t think there are any exceptions thrown upon start, but you can check if it failed:

if (NetworkManager.Singleton.StartHost() == false)
    Debug.LogError("..");

Normall you don’t need a detailed failure message. Check if something like “port already bound” gets logged to player.log. If so, users will figure that out. On the UI side it would suffice to tell “StartHost failed, check player.log”.

You should implement OnTransportFailure because these can occur at any time, ie LAN cable unplugged. But again nothing specific in there.

Oh, i see! I checked everywhere for a callback onError, but forgot to just check the result of it. Thanks for the quick reply!

I’m encountering a similar scenario but for StartClient instead of StartHost. I want to handle failed connections with StartClient because it’s causing my game to freeze. However, I cannot find a way to do this.

As mentioned in this post (Handling Connection Errors), the return from StartClient doesn’t indicate successful connection. That same post said you can use the OnClientDisconnectCallback, but this did not work in my case. It never gets invoked.

I also tried OnTransportFailure (and other callbacks just to make sure) but these didn’t work for me, either.

A socket error and socket warning are logged, but these are not catchable since none of my code is part of their call stacks.

If anyone knows how to go about this, please let me know!

Could you provide more details and code? There is a potential for a bug there, but also simply coding the start/stop of a client incorrectly.

For instance, quite often I notice code where StartXXX gets called first and only after that event delegates are registered which, at the very least, would make OnXXXStarted unavailable.

In my experience the callbacks are very reliable. You do have to subscribe to all potential failure points ie disconnect and transport failure need both be handled, as a transport failure can always occur. If it does, NetworkManager shuts down automatically.

When using custom transports I suppose there is no guarantee that they will correctly bubble up any errors. If that is the case, you could simply start a timer and if within 30 seconds the client connected callback didn’t run you can consider the connections stale and shut down.