Getting immediately disconnected.

Options
Hello,

My LoadBalancingClient seems to be disconnecting right after it connects (receiving StatusCode.Disconnect within a second or so of receiving a StatusCode.Connect.) Does anyone know what the common causes of this behavior is so I can get an idea of where to start looking for the problem in my code?

Thanks in advance for any help!

Comments

  • Tobias
    Options
    Did you check the console log? Anything in there?
    Do you use the cloud? Did you set a AppId?
  • duggo42
    Options
    Unfortunately, the console log is only showing the Debug.Log() output I call in OnStatusChange() when StatusCode.Connect & StatusCode.Disconnect are received. Here is that output if it helps:
    11:27:25.5508430: Client Connected.
    UnityEngine.Debug:Log(Object)
    MultiplayerClient:OnStatusChanged(StatusCode) (at Assets/_InHouse/Scripts/MultiplayerClient.cs:88)
    ExitGames.Client.Photon.PeerBase:InitCallback()
    ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[])
    ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()
    ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()
    ExitGames.Client.Photon.PhotonPeer:Service()
    ExitGames.Client.Photon.LoadBalancing.LoadBalancingClient:Service() (at Assets/Photon/LoadbalancingApi/LoadBalancingClient.cs:655)
    OnlineMultiplayer:Update() (at Assets/_InHouse/Scripts/OnlineMultiplayer.cs:47)
    
    11:27:26.5557280: Client Disconnected.
    UnityEngine.Debug:Log(Object)
    MultiplayerClient:OnStatusChanged(StatusCode) (at Assets/_InHouse/Scripts/MultiplayerClient.cs:99)
    ExitGames.Client.Photon.EnetPeer:Disconnect()
    ExitGames.Client.Photon.PhotonPeer:Disconnect()
    ExitGames.Client.Photon.LoadBalancing.LoadBalancingClient:Disconnect() (at Assets/Photon/LoadbalancingApi/LoadBalancingClient.cs:614)
    ExitGames.Client.Photon.LoadBalancing.LoadBalancingClient:OnOperationResponse(OperationResponse) (at Assets/Photon/LoadbalancingApi/LoadBalancingClient.cs:1611)
    MultiplayerClient:OnOperationResponse(OperationResponse) (at Assets/_InHouse/Scripts/MultiplayerClient.cs:50)
    ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[])
    ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()
    ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()
    ExitGames.Client.Photon.PhotonPeer:Service()
    ExitGames.Client.Photon.LoadBalancing.LoadBalancingClient:Service() (at Assets/Photon/LoadbalancingApi/LoadBalancingClient.cs:655)
    OnlineMultiplayer:Update() (at Assets/_InHouse/Scripts/OnlineMultiplayer.cs:47)
    

    App ID is being set when the client is being instantiated. Here's what that looks like in my code:
    	public void CreateClient () {
    		_client = new MultiplayerClient (); //MultiplayerClient inherits from LoadBalancingClient
    		_client.ConnectToRegionMaster ("us");
    		_client.MasterServerAddress = _cloudServerUrl;
    		_client.AppId = photonAppID;
    		_client.AppVersion = "1.0";
    		_client.PlayerName = testName;
    		_client.titleDisplayName = _accountInfo.TitleInfo.DisplayName;
    		_client.Connect ();
    		
    		ExitGames.Client.Photon.LoadBalancing.AuthenticationValues authenticationValues = new ExitGames.Client.Photon.LoadBalancing.AuthenticationValues ();
    		authenticationValues.AuthParameters = null;
    		authenticationValues.Secret = null;
    		authenticationValues.AuthType = new ExitGames.Client.Photon.LoadBalancing.CustomAuthenticationType ();
    		_client.CustomAuthenticationValues = authenticationValues;
    		
    		_client.CustomAuthenticationValues.SetAuthParameters (_accountInfo.Username, _playFabSessionTicket);
    		
    		_client.JoinLobby ();
    	}
    

    If by cloud, do you mean a CloudServer? If so, yes; I'm using ther service provided by PlayFab, but I'm only using the default Cloud Script they provided right now and I haven't touched that.
  • Tobias
    Options
    Interesting. The LoadBalancingClient calls Disconnect(), so the question is: What's making it do that?
    In LoadBalancing.LoadBalancingClient:OnOperationResponse(OperationResponse), please log the response.ToStringFull(). Also log the LoadBalancingClient.DisconnectedCause.
    There should be a DebugReturn for errors with Authentication. Maybe you don't log those.
    Maybe your app's server-side setup requires Custom Authentication with the PlayFab user accounts. Then you are probably just rejected for some reason.
  • duggo42
    Options
    Hey, Thanks Tobias.

    I Logged the DisconnectedCause like you suggested and got this:
    14:52:59.3253430: Client Disconnected.
    DisconnectCause=CustomAuthenticationFailed
    UnityEngine.Debug:Log(Object)
    MultiplayerClient:OnStatusChanged(StatusCode) (at Assets/_InHouse/Scripts/MultiplayerClient.cs:100)
    ExitGames.Client.Photon.EnetPeer:Disconnect()
    ExitGames.Client.Photon.PhotonPeer:Disconnect()
    ExitGames.Client.Photon.LoadBalancing.LoadBalancingClient:Disconnect() (at Assets/Photon/LoadbalancingApi/LoadBalancingClient.cs:614)
    ExitGames.Client.Photon.LoadBalancing.LoadBalancingClient:OnOperationResponse(OperationResponse) (at Assets/Photon/LoadbalancingApi/LoadBalancingClient.cs:1611)
    MultiplayerClient:OnOperationResponse(OperationResponse) (at Assets/_InHouse/Scripts/MultiplayerClient.cs:50)
    ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[])
    ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()
    ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()
    ExitGames.Client.Photon.PhotonPeer:Service()
    ExitGames.Client.Photon.LoadBalancing.LoadBalancingClient:Service() (at Assets/Photon/LoadbalancingApi/LoadBalancingClient.cs:655)
    OnlineMultiplayer:Update() (at Assets/_InHouse/Scripts/OnlineMultiplayer.cs:47)
    

    And logging OnOperationResponse outputs this:
    14:52:59.3279330: OperationResponse=OperationResponse 230: ReturnCode: 32755 (Invalid username). Parameters: {}
    UnityEngine.Debug:Log(Object)
    MultiplayerClient:OnOperationResponse(OperationResponse) (at Assets/_InHouse/Scripts/MultiplayerClient.cs:52)
    ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[])
    ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()
    ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()
    ExitGames.Client.Photon.PhotonPeer:Service()
    ExitGames.Client.Photon.LoadBalancing.LoadBalancingClient:Service() (at Assets/Photon/LoadbalancingApi/LoadBalancingClient.cs:655)
    OnlineMultiplayer:Update() (at Assets/_InHouse/Scripts/OnlineMultiplayer.cs:47)
    

    Which is weird because I set up my code as they explained on their site (here: https://playfab.com/using-photon-playfab), which looks like this:
    	public void CreateClient () {
    		_client = new MultiplayerClient ();   //MultiplayerClient inherits from LoadBalancingClient
    		_client.ConnectToRegionMaster ("us");
    		_client.MasterServerAddress = _cloudServerUrl;
    		_client.AppId = photonAppID;
    		_client.AppVersion = "1.0";
    		_client.PlayerName = _accountInfo.Username;
    		_client.titleDisplayName = _accountInfo.TitleInfo.DisplayName;
    
    		_client.CustomAuthenticationValues = new ExitGames.Client.Photon.LoadBalancing.AuthenticationValues () {
    			AuthType = ExitGames.Client.Photon.LoadBalancing.CustomAuthenticationType.Custom,
    			Secret = null,
    			AuthParameters = null
    		};
    
    		_client.CustomAuthenticationValues.SetAuthParameters (_accountInfo.Username, _playFabSessionTicket);
    		
    		_client.Connect ();
    
    		_client.JoinLobby ();
    	}
    

    _accountInfo and _playFabSessionTicket are retrieved from PlayFab after the user successfully logs in there, so I'm fairly certain those values are correct, so did I miss something in their explanation?

    Thanks again!
  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    @duggo42 :
    from the logs it is clear that the _accountInfo.Username is the root of the problem.
    You should use the PlayFabId instead of _accountInfo.Username (no need to call GetAccountInfo for Photon authentication) ! In PlayFab - Photon integration, the PlayFab Id is Photon's UserId.

    [extra]
    Check the "username" value from PlayFab's GameManager (from Players tab, knowing the PlayFab ID of the player). Sometimes it's null ! When you're signed in using 3rd party like facebook for instance. Same for DisplayName, it's null unless explicitly set to a value using an API call.
  • duggo42
    Options
    Hi JT, thanks for the suggestion.

    I've changed:
    _client.CustomAuthenticationValues.SetAuthParameters (_accountInfo.Username, _playFabSessionTicket);
    

    To:
    _client.CustomAuthenticationValues.SetAuthParameters (_accountInfo.PlayFabId, _playFabSessionTicket);
    

    I'm still getting immediately disconnected, but now getting this error instead: "OperationResponse=OperationResponse 230: ReturnCode: 32755 (Invalid token). Parameters: {}"

    _accountInfo.PlayFabId matches the the player's PlayFabID given in the GameManager Players panel (also, no Username or DisplayName information is missing for that player.) _playFabSessionTicket matches the value returned in PlayFab.ClientModels.LoginResult.SessionTicket.

    Also, tried:
    _client.CustomAuthenticationValues.SetAuthParameters (_accountInfo.PlayFabId, PlayFabClientAPI.AuthKey);
    

    Same result ("invalid token.")
  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    Hi duggo42,

    You are using the wrong Session Token.
    You have mistaken PlayFab's own session token for PlayFab's Photon custom authentication Token. They are completely different.

    You should use the token returned with GetPhotonAuthenticationToken.

    The workflow to connect to Photon using PlayFab as custom authentication provider :
    1. Sign in to PlayFab
    2. Get photon token using dedicated API call
    3. set photon authentication values : username = playfabId, token = token you got in 2
    4. connect to photon

    Here's a snippet from my own code to give you an idea. (remove all extra stuff)
        public bool PhotonConnect() {
            if (String.IsNullOrEmpty(playFabId)) {
                //Debug.LogWarning("PlayFab Id is null or empty");
                return false;
            }
            else if (String.IsNullOrEmpty(photonToken)) {
                //Debug.LogWarning("PhotonToken Id is null or empty");
                return false;
            }
            else if (photonManager.IsConnected) {
                if (photonManager.UserId.Equals(playFabId)) {
                    Debug.LogWarning("Already connected to the same user");
                    return true;
                }
                else {
                    Debug.LogWarning("Disconnecting previous user");
                    photonManager.Disconnect();
                }
            }
            return photonManager.AuthenticateUser(playFabId, photonToken);
        }
    
        private void GetPhotonToken() {
    #if PHOTON_PUBLIC_CLOUD
            return;
    #else
            GetPhotonAuthenticationTokenRequest request = new GetPhotonAuthenticationTokenRequest();
            request.PhotonApplicationId = GameConstants.PhotonAppId.Trim();
            // get an authentication ticket to pass on to Photon
            PlayFabClientAPI.GetPhotonAuthenticationToken(request, OnPhotonAuthenticationSuccess, OnPlayFabError);
    #endif
        }
    
        // callback on successful GetPhotonAuthenticationToken request
        private void OnPhotonAuthenticationSuccess(GetPhotonAuthenticationTokenResult result) {
            this.photonToken = result.PhotonCustomAuthenticationToken;
            Utils.Log("photon:{0}", this.photonToken);
            if (!PhotonConnect()) {
                Debug.LogWarning("Failed to connect to Photon");
            }
        }
    
  • duggo42
    Options
    Argh, why would they (https://playfab.com/using-photon-playfab) say to use the Session Ticket when you're supposed to use the Photon Authentication Token???

    That's great help, JT, it looks like I'm finally staying connected. Thank you very much! :D
  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    You're welcome.
    I'm glad I could help.