Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

how to handle "failed to connect to master server"?

Discussion in 'Scripting' started by sandbaydev, Jun 20, 2014.

  1. sandbaydev

    sandbaydev

    Joined:
    Aug 9, 2013
    Posts:
    104
    I'm getting an error and I wouldnt want this error to stop the game. How should I handle registering master servers?

    I occasionally get this error:
    Code (csharp):
    1. "Failed to connect to master server at 67.225.180.24:23466"
    I have this...
    Code (csharp):
    1.  
    2.     void OnFailedToConnectToMasterServer(NetworkConnectionError info) {
    3.         Debug.Log("Could not connect to master server: " + info);
    4.     }
    5.  
    And I also have the following:
    Code (csharp):
    1.  
    2.         try {
    3.             MasterServer.RegisterHost(typeName, roomName, version);
    4.             Debug.Log ("registered master host...");
    5.         } catch (Exception e) {
    6.             Debug.Log ("failed to register master host: "+ e.Message);
    7.         }
    (alternatively, I tried this too:)
    Code (csharp):
    1.  
    2.         try {
    3.             MasterServer.RegisterHost(typeName, roomName, version);
    4.             Debug.Log ("registered master host...");
    5.         } catch (UnityException e) {
    6.             Debug.Log ("failed to register master host: "+ e.Message);
    7.         }
    8.  
    But for some reason, no matter how much error checking I put there, the game stops when you cannot connect to the master server.

    I don't need "alternative master server" or stuff like that, but
     
  2. sandbaydev

    sandbaydev

    Joined:
    Aug 9, 2013
    Posts:
    104
    I know realize, the problem happens before the master server thingies... the problem occurs if I use NAT. So... how to handle things so that even if handshaker cannot be used, it wouldn't stop the game.

    the following won't work:
    Code (csharp):
    1.  
    2. Network.InitializeServer(1, 20000, !Network.HavePublicAddress());
    3.  
    and this works:
    Code (csharp):
    1.  
    2. Network.InitializeServer(1, 20000, false);
    3.  
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    What do you mean by "stops the game"? I've got pretty strict error handling in place that displays exceptions in a GUI and I don't recall this popping up - which would explain why your catch isn't working because it wouldn't actually be a thrown exception.

    That being said - Network.Connect doesn't behave well when it fails to connect due to some kind of timeout period. Connect doesn't explicitly throw an exception so wrapping it in a try catch doesn't do anything. (It returns a NetworkConnectionError and then just throws an exception after some timeout period has expired - which is awful.)
     
  4. sandbaydev

    sandbaydev

    Joined:
    Aug 9, 2013
    Posts:
    104
    In Editor mode, I click play, I initialize a host. If I have "nat enabled", after few secs I get Error "failed to connect" and editor play mode stops.

    To my understand Network.Connect does make some sort of error, or at least calls that "connectionfailed"
    http://docs.unity3d.com/ScriptReference/Network.Connect.html
    http://docs.unity3d.com/ScriptReference/NetworkConnectionError.ConnectionFailed.html

    ...like you said. So... how can I prevent this network connect error thing from stopping my editor play :)
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    A network connection error shouldn't dump your editor out of play mode.....

    Also - Connect *should* return a ConnectionFailed enum but it doesn't. It returns NoError and then throws an exception a few seconds later. As far as I can tell the NetworkConnectionError functionality does nothing.
     
  6. sandbaydev

    sandbaydev

    Joined:
    Aug 9, 2013
    Posts:
    104
    Well, I agree, it shouldn't... but it does. If I use NAT/handshaker option (set it True), then I get error few secs later and that stops playmode. (=if the handshaker happens to be offline, of course this works fine when the handshaker is online)

    So, is there anything to do to handle the error?
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I've never come across that before. If it dumps your editor out of play mode then what does it do in a built client?
     
  8. CoventrySG

    CoventrySG

    Joined:
    Jun 12, 2013
    Posts:
    3
    Hi, no answers here, but running into the same problem. Currently running a local master server (not modified from Unity's sources), but it does not matter which master server I use, Unity's or my own.

    If "Failed to connect to master server at 67.225.180.24:23466" comes out, because it is red text (i.e a crash), Unity networking will simply not work anymore until the game is closed and restarted.

    (Naturally, this is more difficult to do in iOS, because Apple discourages exiting apps completely.)

    This occurs even if you override OnFailedToConnectToMasterServer().

    It doesn't dump you out of editor or the game, but it means network functionality is crippled if no master server is detected.

    I've poured through the help files and can't find any method to restart Networking without exiting the game.
     
  9. sandbaydev

    sandbaydev

    Joined:
    Aug 9, 2013
    Posts:
    104
    @KelsoMRK: Erhm... *Unity n00b warning ahead* I just realized that it simply pauses the game *whistling* in-editor... which means I can click (=deselect) pause button and continue to play.

    It does nothing on game mode... I can keep playing. Thanks mister.

    So, the solution is:
    - If I get error, I click Pause button so that I can continue playing
    - For actual play mode: I can handle the warning using OnConnectionFailed, and then do something (like change state of the game accordingly & tell player that "sry, but master server has vanished".

    And then of course should try establish master server connection again. (Won't deal with that now, my main problem in this thread was about handling the error properly)
     
  10. sandbaydev

    sandbaydev

    Joined:
    Aug 9, 2013
    Posts:
    104
    Haven't tried, but in theory... can't you simply try do Network.Connect again after failure?
     
  11. CoventrySG

    CoventrySG

    Joined:
    Jun 12, 2013
    Posts:
    3
    I can't, Network has already "crashed", so that won't work anymore until I restart.

    This also happens on the server end - it uses Network.InitializeServer( <connection>, <listenport>, <publicaddressbool>).

    The statement itself does not throw the "Failed to connect to master server at..." error, which appears only after the Server is *successfully created*. The error is thrown AFTER the server is created, or at least whenever the master server is queried.

    OnFailedToConnectToMasterServer() runs only after the red text is generated and does not seem to catch anything.

    This means I now have a client that no longer works after going into the Lobby to look for servers.

    Also, this means I now have a server that cannot be detected by its clients.

    It also means I cannot try to reconnect or set up the server until I exit the game, because Network.InitializeServer or Network.Connect will no longer work.

    PS: the error you're getting comes out only when Unity turns its own test Master Server off, which does happen once in awhile. You're not meant to use the Unity Master Server in release apps; you'll need to run your own copy.
     
  12. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You have to call Disconnect before attempting to connect again. It sounds weird because technically you're not actually connected but Unity is creating the "server" in the host client which needs to be cleaned up.
     
  13. PogyManTV

    PogyManTV

    Joined:
    Mar 25, 2015
    Posts:
    4
    Did anyone solved this? I mean it's simple is there something like "MasterServerReconnect()" function? If not what alternatives can be used?
     
  14. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    This networking framework isn't even supported anymore
     
  15. PogyManTV

    PogyManTV

    Joined:
    Mar 25, 2015
    Posts:
    4
    I know but I'm still using unity 4 and old netoworking API, so it would be cool to get some solutions :D you know