Stuck on ConnectingToNameServer. App placed in background does not reconnect to master server.

I'm running into a problem where the app goes into the background and disconnects. When I refocus I check if we are disconnected and reconnect. The problem is that it only reconnects tot the nameserver and not the master server. When I try to connect I cannot because PeerState is not Disconnected.

PhotonNetwork.networkingPeer.PeerState == Connected
PhotonNetwork.connected == false
PhotonNetwork.connectionStateDetailed == ConnectedToNameServer

Comments

  • I notice when I call connect it calls this function

    public bool ConnectToRegionMaster(CloudRegionCode region)
    {
    ....
    this.State = ClientState.ConnectingToNameServer;

    Then it gets stuck in the that state of ConnectingToNameServer
  • This seems related to calling connect right after they have been disconnected.
  • If you didn't update to the latest PUN, please do. Let us know if this still happens.
  • notdoppler
    edited November 2016
    Hi Tobias. Sorry I should have included my version number. I'm using photon PUN+ 1.78. On iOS and Android platforms. It also occurs in the Unity editor. Could it be related to multiple sequential calls to ConnectUsingSettings or calling Disconnect then immediately calling ConnectUsingSettings?
  • I am also experiencing this issue attempting to implement region switching in our server browser. Some regions work, others will indefinitely get stuck on "ConnectingToNameServer", perhaps I am going about switching regions wrong. I'm also running PUN+ 1.78.

    https://gyazo.com/50a49da3ec0780d4dff29cf75e8cc22e
    https://gyazo.com/1a00f07b1ff74953f344e4c4921cbeb5
  • This often happens on the second time I try to reconnect.
  • JohnTube
    JohnTube ✭✭✭✭✭
    edited November 2016
    Hi @Nobrega, @notdoppler,

    Please wait until Photon is "fully" disconnected to be able to reconnect so for instance you could do something like (code not tested, combined both use cases):
    bool reconnectWhenUnpaused;
    bool reconnectToSwitchRegion;
    CloudRegionCode targetRegion;
    CloudRegionCode currentRegion; 
    
    void OnApplicationPause(bool paused) {
         if (paused) {
              if (isConnected()) { // TODO: implement this?
                  reconnectWhenUnpaused = true;
                  targetRegion = currentRegion;
                  PhotonNetwork.Disconnect();
              }
         } else if (reconnectWhenUnpaused) {
              PhotonNetwork.ConnectToRegion(targetRegion, version);
         }
    }
    
    
    void ChangeRegion(CloudRegionCode region){
          if (currentRegion != region) {
               targetRegion = region;
               reconnectToSwitchRegion = true;
               PhotonNetwork.Disconnect();
          }
    }
    
    void OnDisconnectedFromPhoton() {
           if (reconnectToSwitchRegion){
               PhotonNetwork.ConnectToRegion(targetRegion, version);
           }
    }
    
    
    void OnConnectedToMaster() {
          reconnectWhenUnpaused = false;
          reconnectToSwitchRegion = false;
          currentRegion = targetRegion; // TODO: this requires using targetRegion on fist connection as well.
    }
    
    void OnJoinedLobby(){
          OnConnectedToMaster();
    }
  • Thanks JohnTube I'll give your code a try. I'm pretty sure I wait now for it to reconnect. Basically I disconnect and I have some thing like a coroutine that waits until the PhotonNetwork.connected/detailedconnection state is disconnected. Then I add in a 1 second buffer time to Reconnect. I still get Photon in that strange state of ConnectingToNameServer.

    Even though if this was an issue with reconnecting too fast I think it is still a bug if Photon is stuck on ConnectingToNameServer without giving any kind of error or warning message.
  • Hi,

    I am having the same issue with not being able to reconnect after my app loses focus due to a Unity Ads taking focus.

    In OnApplicationPause(true) I am forcing photon to disconnect via PhotonNetwork.Disconnect ().

    When my app regains focus, I then kick off a coroutine with the following code:
    private IEnumerator ReconnectAFterAdvert()
    {
    	while (PhotonNetwork.connectionState != ConnectionState.Disconnected)
    	{
    		Dbg.Trace ("Waiting for disconnect");
    		yield return null;
    	}
    
    	Dbg.Trace ("Reconnecting to PUN after advert");
    	Network_Manager.Instance.StartConnecting();
    
    	while (	!PhotonNetwork.connectedAndReady &&
    			!Network_Manager.Instance.ConnectionAttemptFailed)
    	{
    		Dbg.Trace ("Waiting for reconnect");
    		Dbg.Trace ("PhotonNetwork.connectionState: " + PhotonNetwork.connectionState);
    		Dbg.Trace ("PhotonNetwork.connectionStateDetailed: " + PhotonNetwork.connectionStateDetailed);
    		yield return null;
    	}
    
    	Dbg.Trace ("Reconnected to PUN!");
    }

    PhotonNetwork.connectionState reports "Connected", PhotonNetwork.connectionStateDetailed reports "ConnectedToNameServer". PhotonNetwork.connectedAndReady never returns true and my game waits indefinitely.

    I imagine there is a fix/workaround for this issue in the 2 years since it was originally reported but I haven't been able to find it yet!

    I am using PUN 1.91 (the last of the 1 series?) and unfortunately am not in a position to upgrade to PUN 2 as of now.

    Any help at all is appreciated.

    Many thanks,

    Niall
  • I am getting the same outcome like you, even in PUN 2 using Unity 2021.3.21f1


    After the first disconnect after beeing in the lobby, a further connect into the lobby isn't possible.

    "ConnectingToNameServer" forever. I also tried to connect again and also Call JoinLobby() without changes, also after waiting for a long time.

  • Thanks for mentioning the versions.

    Please post in a new thread and let us know what you do exactly. The sequence of actions is important here. Usually, PUN is able to disconnect and connect again any number of times but maybe there is a specific sequence of calls that causes this issue.

    However, we can't discuss this in this very old thread involving PUN 1.

This discussion has been closed.