Search Unity

Handling Connection Errors

Discussion in 'Netcode for GameObjects' started by Xefan, Nov 26, 2021.

  1. Xefan

    Xefan

    Joined:
    Jul 2, 2014
    Posts:
    3
    Has anyone got any pointers on how to handle connection errors on initial connect?

    If I run StartClient, the return value isn't trustworthy, Unity Transport returns true even on failure to start the client and connect. UNetTransport at least responds accurately here with false.

    However, both transports output errors into the log, but neither seem to return these errors, or throw them as exceptions to catch; they just catch them and silently fail.

    Is there really no way to capture connection failure errors? Is it really a case of having to just run a timer and if we're not connected by a certain time then throw a generic error to the user with no hint as to what's happened as in the Boss Room example?

    To be clear about what I'm saying, it's easy to test - run start client against a non-existent address, with each transport you'll get an error, but neither passes that error upto the caller of StartClient as far as I can tell.

    Am I missing something obvious here or does NetCode for GameObjects just have no error response for things like connection errors?
     
  2. FionaSarah

    FionaSarah

    Joined:
    Aug 26, 2015
    Posts:
    11
    I'd love an answer on this one too. I'm really surprised by the lack of exception throwing.
     
  3. gralmurdok

    gralmurdok

    Joined:
    Nov 15, 2021
    Posts:
    2
    please, I'm dealing with this issue as well, any help would be appreciated!
     
  4. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    Before calling StartClient, on loading your connection script (e.g. in OnEnable) subscribe to
    OnClientDisconnectCallback
    . For example, write a function called
    HandleClientDisconnected
    :
    Code (CSharp):
    1. NetworkManager.Singleton.OnClientDisconnectCallback+=HandleClientDisconnected;
    This function will be called when a client successfully connected and then disconnected, but also when the connection failed for any reason (e.g. invalid address, timeout). By setting a local variable on successful connection, you can distinguish between these two cases.