PhotonNetwork.connectedAndReady Problem

Options
Hi,

I've got a problem when users go to background on mobile and come back. Photon gets disconnected after some time, which is expected. The problem is that I try reconnecting and I can see PhotonNetwork.connectionState changing to "Connecting" and then "Connected". Howerever PhotonNetwork.connectedAndReady will never be true, and I can't join any rooms.

Any ideas?

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    Hi @Raúl,

    Thank you for choosing Photon!

    Are you still using PUN v1.80? Did you try using PUN v1.83?

    It seems easy to reproduce and it is strange I don't recall reports about this exact scenario. This one is similar maybe (disconnect is due to timeout).

    Did you try calling explicit disconnect in OnApplicationPause(true)?
  • Raúl
    Options
    Hi,

    I'm using 1.80, could you reproduce? I'll change to 1.83 but that big of a change needs proper testing, I wanted to know if I was doing something wrong.

    I'll also try to disconnect when application is paused, but I see this as a small temporal workaround, since is not a real solution to the problem.

    Thanks
  • Raúl
    Options
    Changed to 1.83 still the same problem.

    Disconnecting on OnApplicationPause does not help either. I get disconnected and can't connect back.

    After disonnecting I call ConnectToBestCloudServer() and it goes from disconnected to connecting to connected, but PhotonNetwork.connectedAndReady is still false and can't join any rooms.

    ConnectToBestCloudServer() failed. Can only connect while in state 'Disconnected'. Current state: Connected (and yet PhotonNetwork.connectedAndReady is false!)

    Can anyone help? this is a pretty serious issue for me.
  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    Hi again @Raúl,

    We received your email.
    We will try to investigate and reproduce.
  • Raúl
    Options
    Hi,

    Thank you very much. I think i'm onto something. It seems that manipulating the value of PhotonNetwork.isMessageQueueRunning breaks things, even if you set it back to true.

    What i was doing is PhotonNetwork.isMessageQueueRunning = false when user goes to background and = true when he comes back, removed this logic (I no longer manipulate PhotonNetwork.isMessageQueueRunning and it seems to work in most cases, I'm going to keep investigating this and get back to you with any updates.

  • Raúl
    Options
    Also I was using this cause some doc said that we should set this to false when scenes are loaded, is that correct?
  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    Yes that is correct as stated here.
    So you are saying this issue happens when app goes to background during scene loading?
  • jeanfabre
    Options
    Hi,

    Are you using authentication and/or offline mode in your game?

    Bye,

    Jean
  • Raúl
    Options
    This is happening when user disconnects, I detected it on "going to background for too long" disconnections. Scene loading works fine cause I don't lose my connection. Actively disconnecting when going to background and trying to reconnect again doesn't work if I manipulate isMessageQueueRunning.
  • Raúl
    Options
    And no, no authentication nor offline mode.
  • jeanfabre
    Options
    ok,

    - and what are you experiencing is you leave isMessageQueueRunning to true?
    - are you connecting to the lobby or directly to the masterServer?

    Bye,

    Jean
  • Raúl
    Options
    - When I have this on OnApplicationPause(): https://puu.sh/w6y7u/44449a141f.png is when I have the issue.


    First time I connect to Photon I do this:
    PhotonNetwork.autoJoinLobby = true;
    PhotonNetwork.ConnectToBestCloudServer(PhotonNetwork.gameVersion);

    When connection is lost I just do this:
    PhotonNetwork.ConnectToBestCloudServer(PhotonNetwork.gameVersion);
  • jeanfabre
    Options
    Hi,

    Ok, we are currently investigating the issue, sorry for the delay, it seems there is an issue indeed.

    Bye,

    Jean
  • jeanfabre
    Options
    Hi,

    ok, so It works if you do the following:

    -use

    PhotonNetwork.AuthValues = new AuthenticationValues ();
    PhotonNetwork.AuthValues.UserId = PlayerID;

    before connecting so that each player have a unique ID. It must be the same when reconnecting and rejoining obviously. Usually if you simply ask for the player name, then you set the UserId to be the same and you are good to go.

    - set the PlayerTtl ( Player Time to Live) of the room properties to something bigger than the background timeout, basically how long you give to the user when app is paued before truly disconnecting.

    - use PhotonNetwork.BackgroundTimeout = 10f;

    or any value, 60s is too much indeed, you can make it even 1s if you want,

    with this reconnect and rejoin works

    experiment with this and let me know how it goes.

    I filed a ticket internally to check if PlayerTtl is a must for reconnect and rejoin or if indeed it shoudl work without any PlayerTtl set.


    Bye,

    Jean
  • Raúl
    Options
    Hey Jean,

    Thank you for looking into this. I will definitely try this ASAP and will get back to you.
  • Raúl
    Raúl
    edited June 2017
    Options
    Hi Jean,

    I tried what you said and it's not working for me. I'll paste my connect code:
    string fb_id = "";
    if(FacebookFacade.userFBId != null)
    	fb_id = FacebookFacade.userFBId.ToString();
    
    ExitGames.Client.Photon.Hashtable someCustomPropertiesToSet = new ExitGames.Client.Photon.Hashtable () 
    {
    	{"league", User.userDivision},
    	{"stars", User.stars},
    	{"fbId", fb_id},
    	{"name", User.name},
    	{"userId",User.id},
    	{"pais","ES"}
    };
    
    string playerName = User.name;
    Services s = FindObjectOfType<Services>();
    PhotonNetwork.player.SetCustomProperties(someCustomPropertiesToSet);
    
    PhotonNetwork.BackgroundTimeout = 10f;
    
    PhotonNetwork.AuthValues = new AuthenticationValues ();
    PhotonNetwork.AuthValues.UserId = User.id.ToString();
    
    PhotonNetwork.gameVersion = "GAME_NAME_"+Application.version;
    
    PhotonNetwork.playerName = playerName;
    
    PhotonNetwork.autoJoinLobby = true;
    //PhotonNetwork.ConnectToBestCloudServer(PhotonNetwork.gameVersion);
    PhotonNetwork.ConnectToRegion(CloudRegionCode.eu,PhotonNetwork.gameVersion);

    Whenever I go into background or start loading a level, I set isMessageQueueRunning to false and when I come back from background or finish loading a level, I set it back to true. This I have checked with debug logs and is correct.

    I fixed the issue on iOS by Disconnecting when going to background and Reconnecting when comming back. However, this only works for iOS and Android still can't reconnect back.

    Is there a workaround? Can I reset the PhotonNetwork class so that it tries to create a clean connection as the first one? Am I doing something wrong?

    Regards,
    Raúl.
  • Raúl
    Options
    Just to be clear, going to background does not get me disconnected, going for too long does though. Which is the expected behaviour. The problem is, whatever way I get disconnected, I can't connect back. Status goes from "Connecting" to "Connected" (thus trying to connect gives an error saying I'm already connected) but PhotonNetwork.connectedAndReady is false.
  • Raúl
    Raúl
    edited June 2017
    Options
    I think I solved it.
    I added a Reconnect call to OnDisconnectedFromPhoton event listener. And I also do a Reconnect call when PhotonNetwork.connectedAndReady is false and a user tries to start a game.
    public override void OnDisconnectedFromPhoton ()
    	{
    		base.OnDisconnectedFromPhoton ();
    		Debug.Log("Lost connection to photon");
    		PhotonNetwork.Reconnect();
    }

    I believe the main problem here was to start a new connection instead of calling Reconnect. This isn't so obvious to me though, creating a new connection should be possible or warning/error messages should pop up when trying to do so.
  • Raúl
    Raúl
    edited June 2017
    Options
    Also, these two lines fix an issue when Reconnecting after a long period of disconnection (auth token expires):
    PhotonNetwork.AuthValues = new AuthenticationValues ();
    PhotonNetwork.AuthValues.UserId = User.id.ToString();
    
    Just posting this in case someone faces this problem in the future.
  • jeanfabre
    Options
    Hi,

    Good.

    - I got the confirmation that indeed everything is good with PlayerTtl being mandatory for reconnection and rejoin.

    - yes if the auth token expired you need to renew it that's true for any situation.

    - yes going to bg only gets you disconnected after the timeout set with PhotonNetwork.BackgroundTimeout but in some cases you might want to set this timeout to a very very low value, like 1 seconds because indeed the user is gone and might not comeback at all, so waiting 10 seconds is a lot in this case for most games.

    - as for the connection status, we did find some inconsistency when you check for some connection status, this is fixed internally and will be there for the next update, however, all photon callbacks work fine so rely on them instead of variables to be sure while waiting for the next version on the asset store.


    Bye,

    Jean