Photon stuck in connecting state.

Receiving the following error in PUN2.

ConnectUsingSettings() failed. Can only connect while in state 'Disconnected'. Current state: Connecting

State never changes even after trying to disconnect.

Comments

  • I'm still new myself but I will try to help out as much as possible.

    Any errors?
  • Thanks so much. No errors just the warning.

    Coming from offlineMode = true, which I'm assuming is why it's flagging it as connected.
  • Can you post your code?
  • meatloaf
    meatloaf
    edited October 2018
    Its a bit much, but essentially it's something like this.

    Connection Code
      
        /// <summary>
        /// Connects to the Photon service. Essentially a wrapper for <see cref="PhotonNetwork.ConnectUsingSettings"/>
        /// <para>If in offlineMode will disable offline mode (set OfflineMode = false)</para>
        /// </summary>
        /// <param name="successCallback">Fires when we connect to master.</param>
        /// <param name="errorCallback">Fires when we cannot connect to master.</param>
        public void ConnectToPhoton(Action successCallback, Action errorCallback) {
            if (InitializeError()) {
                return;
            }
    
            connectingToPhoton = true;
            Action successHandler = () => { };
            successHandler = () => {
                OnConnectedToMasterInternal -= successHandler;
                connectingToPhoton = false;
                successCallback();
            };
            OnConnectedToMasterInternal += successHandler;
    
            if (!PhotonNetwork.ConnectUsingSettings()) {
                OnConnectedToMasterInternal -= successHandler;
                connectingToPhoton = false;
                errorCallback();
            }
        }
    
        /// <summary>
        /// Connects player to a local offline room for use with singleplayer
        /// </summary>
        /// <param name="roomName">The name of the offline room</param>
        /// <param name="successCallback">Called when a room is successfully connected to. Returns the current player count.</param>
        /// <param name="errorCallback">
        /// <para>Called when a room fails to be connected to. Returns the error code.</para>
        /// <para>Disconnect case must be handled separately.</para>
        /// </param>
    public void ConnectToOfflineRoom(string roomName, Action<int> successCallback, Action<short> errorCallback) {
            if (InitializeError()) {
                return;
            }
            if (PhotonNetwork.OfflineMode) {
                Debug.Log("Offline mode already set to true, will attempt to join offline room");
            }
            
            if (PhotonNetwork.InRoom) {
                Debug.LogWarning("Can't connect to offline room, you are in one... Room Name: " + PhotonNetwork.CurrentRoom.Name);
                return;
            }
            
            connectingToOfflineRoom = true;
            Action<int> successHandler = (players) => {};
            Action<short> errorHandler = (returnCode) => {};
            Action onComplete = () => {
                OnJoinedRoomInternal -= successHandler;
                OnCreateRoomFailedInternal -= errorHandler;
                connectingToOfflineRoom = false;
            };
            successHandler = (players) => {
                onComplete();
                Debug.LogFormat("Offline room successfully joined...");
                successCallback(players);
            };
            errorHandler = (returnCode) => {
                onComplete();
                Debug.LogFormat("Error offline joining room. Failed with error code {0}...",returnCode);
                errorCallback(returnCode);
            };
            
            OnJoinedRoomInternal += successHandler;
            OnCreateRoomFailedInternal += errorHandler;
    
            PhotonNetwork.OfflineMode = true;
            PhotonNetwork.CreateRoom(roomName, new RoomOptions {
                IsVisible = false,
                MaxPlayers = 1
            });
        }
    
    Pre Connecting Stuck
    photonConnectionService.ConnectToOfflineRoom("Single Player",
        successCallback: (playerCount) => {
            PresentCanvasController<CharacterSelectCanvasController>(
                configuration: (characterSelectCanvasController) => {
                    characterSelectCanvasController.Configure(false);
                    });
        },
        errorCallback: (disconnectCause) => { });
    Post Connecting Stuck
    
    photonConnectionService.value.ConnectToPhoton(
        successCallback: () => { },
        errorCallback: () => { });
    

    When Post Connecting Stuck Code is executed it is coming from PhotonNetwork.OfflineMode = true. This is when the error is displayed.
  • So specifically I have found that it gets stuck at the ConnectingToNameServer state.
  • Holy crap! Figured this one out.

    It's because I was exiting from a pause state which had set Time.timeScale = 0;. I wasn't resetting it back to Time.timeScale = 1; before I quit from the pause state. I was unaware that timeScale also effected Photons ability to connect.

    I'm wondering if anyone else has been confused by this as well or if i'm the only dummy.

    Sorry for all the confusion. hopefully someone else learns from my mistakes!