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


  • 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.
  • 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.
  • This often happens on the second time I try to reconnect.
  • JohnTubeJohnTube mod
    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;
         } else if (reconnectWhenUnpaused) {
              PhotonNetwork.ConnectToRegion(targetRegion, version);
    void ChangeRegion(CloudRegionCode region){
          if (currentRegion != region) {
               targetRegion = region;
               reconnectToSwitchRegion = true;
    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(){
  • 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");
    	while (	!PhotonNetwork.connectedAndReady &&
    		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,

Sign In or Register to comment.