Help: reconnect and rejoin

Hi, i am new to photon. And my problem is, Whenever i reconnect and rejoin the game using reconnectandrejoin(), the playerlist.length is 0.

Lets say that there are 2 players A and B. And when A rejoins after disconnecting, its playerlist is 0 and masterclient is null.
Similarly, in B the playerlist count does not add up to be 2 and remains 1 as if the player A has not connected.

Eventhough, the reconnectAndRejoin returns true.

If there is a tutorial about rejoining can you please direct me towards it, or can you please show be how to rejoin a player properly.

Answers

  • JohnTube
    JohnTube ✭✭✭✭✭
    Hi @Apabad,

    Thank you for choosing Photon!

    This depends on when you are checking the player list and master client.
    You should always do this when InRoom == true (ClientState == Joined), meaning after OnJoinedRoom callback is called even in case of reconnect and rejoin.
    After disconnection or leaving the room those values are not relevant and do not make sense anymore (even if it's an unexpected disconnect).
  • Thank you for your reply,

    For the checking purpose, i have kept the debug.log(photonnetwork.playerlist.length); in update loop. And when i connect the game using reconnectandrejoin, it returns true value and PhotonNetwork.NetworkingClient.LoadBalancingPeer.PeerState is giving connected as output.

    so, how to rejoin player A to the game, when player A is disconnected from the game?
  • JohnTube
    JohnTube ✭✭✭✭✭
    Hi @Apabad,

    I think you don't need to check LoadBalancingPeer.PeerState or log playerlist.length in Update.
    What you need is to implement callbacks properly and handle recovery from unexpected disconnects.
    You could use this example script:
    using Photon.Realtime;
    using UnityEngine;
    
    
    namespace Photon.Pun.UtilityScripts
    {
        /// <summary>
        /// Unexpected disconnects recovery
        /// </summary>
        public class DisconnectsRecovery : MonoBehaviourPunCallbacks
        {
            private bool rejoinCalled;
    
            private bool reconnectCalled;
    
            private bool inRoom;
    
            private DisconnectCause previousDisconnectCause;
    
            public override void OnDisconnected(DisconnectCause cause)
            {
                Debug.LogFormat("OnDisconnected(cause={0}) ClientState={1} PeerState={2}",
                                cause,
                                PhotonNetwork.NetworkingClient.State,
                                PhotonNetwork.NetworkingClient.LoadBalancingPeer.PeerState);
                if (this.rejoinCalled)
                {
                    Debug.LogErrorFormat("Rejoin failed, client disconnected, causes; prev.:{0} current:{1}", this.previousDisconnectCause, cause);
                    this.rejoinCalled = false;
                }
                else if (this.reconnectCalled)
                {
                    Debug.LogErrorFormat("Reconnect failed, client disconnected, causes; prev.:{0} current:{1}", this.previousDisconnectCause, cause);
                    this.reconnectCalled = false;
                }
                this.HandleDisconnect(cause); // add attempts counter? to avoid infinite retries?
                this.inRoom = false;
                this.previousDisconnectCause = cause;
            }
    
            private void HandleDisconnect(DisconnectCause cause)
            {
                switch (cause)
                {
                    // cases that we can recover from
                    case DisconnectCause.ServerTimeout:
                    case DisconnectCause.Exception:
                    case DisconnectCause.ClientTimeout:
                    case DisconnectCause.DisconnectByServerLogic:
                    case DisconnectCause.AuthenticationTicketExpired:
                    case DisconnectCause.DisconnectByServerReasonUnknown:
                        if (this.inRoom)
                        {
                            Debug.Log("calling PhotonNetwork.ReconnectAndRejoin()");
                            this.rejoinCalled = PhotonNetwork.ReconnectAndRejoin();
                            if (!this.rejoinCalled)
                            {
                                Debug.LogWarning("PhotonNetwork.ReconnectAndRejoin returned false, PhotonNetwork.Reconnect is called instead.");
                                this.reconnectCalled = PhotonNetwork.Reconnect();
                            }
                        }
                        else
                        {
                            Debug.Log("calling PhotonNetwork.Reconnect()");
                            this.reconnectCalled = PhotonNetwork.Reconnect();
                        }
                        if (!this.rejoinCalled && !this.reconnectCalled)
                        {
                            Debug.LogError("PhotonNetwork.ReconnectAndRejoin() or PhotonNetwork.Reconnect() returned false, client stays disconnected.");
                        }
                        break;
                    case DisconnectCause.None:
                    case DisconnectCause.OperationNotAllowedInCurrentState:
                    case DisconnectCause.CustomAuthenticationFailed:
                    case DisconnectCause.DisconnectByClientLogic:
                    case DisconnectCause.InvalidAuthentication:
                    case DisconnectCause.ExceptionOnConnect:
                    case DisconnectCause.MaxCcuReached:
                    case DisconnectCause.InvalidRegion:
                        Debug.LogErrorFormat("Disconnection we cannot automatically recover from, cause: {0}, report it if you think auto recovery is still possible", cause);
                        break;
                }
            }
    
            public override void OnJoinRoomFailed(short returnCode, string message)
            {
                if (this.rejoinCalled)
                {
                    Debug.LogErrorFormat("Quick rejoin failed with error code: {0} & error message: {1}", returnCode, message);
                    this.rejoinCalled = false;
                }
            }
    
            public override void OnJoinedRoom()
            {
                this.inRoom = true;
                if (this.rejoinCalled)
                {
                    Debug.Log("Rejoin successful");
                    this.rejoinCalled = false;
                }
            }
    
            public override void OnLeftRoom()
            {
                this.inRoom = false;
            }
            
            public override void OnConnectedToMaster()
            {
                if (this.reconnectCalled)
                {
                    Debug.Log("Reconnect successful");
                    this.reconnectCalled = false;
                }
            }
        }
    }
    
  • Hey,
    I am using Pun2 in one of my unity application.
    I have implemented a code PhotonNetwork.ReconnectAndRejoin() when the internet connection has been lost for some reasons.
    i have set PlayerTTL also like this:

    PhotonNetwork.CurrentRoom.PlayerTtl = 12000;
    PhotonNetwork.CurrentRoom.EmptyRoomTtl = 12000;

    PhotonNetwork.ReconnectAndRejoin() is working fine to rejoin in room.
    But there are some problems:
    1. After rejoin in room every Npc's positions has been changed, first they are out of view then suddenly they came at centre on the ground.
    2. All objects all players all npc's are instantiated on centre point after reconnect and rejoin and this happens randomly.
    3. Before disconnected from the room due to internet connect the player attacked on Npc's and destroyed that Npc's but after rejoin the room that already destroyed npc's reinstantiated again/or they come again in scene and behaves differently.

    can you please advice something for these problems?

    Thanks
    Sakshi