Handle Disconnect

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Handle Disconnect

Clevereen
2021-01-25 13:47:40

Hello,
i'm using PUN2 and I try to handle disconnection. My goal is to display the player who is having a disconnection issue and wait for him before he is kicked out.

//Internet server  
using Photon.Pun;  
using Photon.Realtime;

public class PhotonRoom : MonoBehaviourPunCallbacks, IInRoomCallbacks  
{



    /// <summary>  
    /// Called when a remote player left the room. This PhotonPlayer is already removed from the playerlist at this time.  
    /// When your client calls PhotonNetwork.leaveRoom, PUN will call this method on the remaining clients.  
     ///  When a remote client drops connection or gets closed,   
    ///this callback gets executed.after a timeout of several seconds.   
    /// </summary>  
    /// <param name="otherPlayer"></param>

    public void OnPhotonPlayerDisconnected(Player otherPlayer)  
    {  
        Debug.Log("Player Disconnected " + otherPlayer.ActorNumber);  
        //If it is me who is having a connexion issue  
        if(otherPlayer.ActorNumber == myGroundPlayer.myActorNumber)  
        {  
            panelAttempt.SetActive(true);  
            playerName.text = myGroundPlayer.myPlayerName;  
        }  
        //If it is the other player who is having a connexion issue  
        else  
        {  
            panelAttempt.SetActive(true);  
            playerName.text = myMedolir.opponentMedolir.GetComponent<MedolirLife>().Tmp_PlayerName.text;  
        }

    }

}  

I read the documentation but somehow, the callback method is never called.

Comments

JohnTube
2021-01-25 23:16:46

Hi @Clevereen,

Thank you for choosing Photon!

  • This discussion was first posted under Photon Voice category, I have moved it to PUN category as I don't see anything Photon Voice specific.
  • MonoBehaviourPunCallbacks class already extends IInRoomCallbacks so no need to implement it in PhotonRoom just override any callback method needed from there.
  • OnPhotonPlayerDisconnected is from PUN classic, in PUN 2 it was replaced with IInRoomCallbacks.OnPlayerLeftRoom. Read more about Callbacks Changes.
  • IInRoomCallbacks.OnPlayerLeftRoom is triggered only for remote players, for the local players you have IConnectionCallbacks.OnDisconnected (or IMatchmakingCallbacks.OnLeftRoom).

Clevereen
2021-01-26 09:47:50

Hi @JohnTube ,

thanks for the reply. I have used the code and so far, it is not really working as intended.

//Internet server  
using Photon.Pun;  
using Photon.Realtime;

public class PhotonRoom : MonoBehaviourPunCallbacks//, IInRoomCallbacks  
{


    /// <summary>  
    /// -CLEV-  
    /// THIS METHOD WILL DISPLAY TO THIS CLIENT THE CONNEXION ISSUE THAT THE OTHER  
    /// PLAYER IS FACING  
    /// (ACCORDING TO THE ACTOR NUMBER MYSELF OR THE OTHER PLAYERS  
    /// </summary>  
    /// <param name="otherPlayer"></param>  
    private void DisplayIssue(Player otherPlayer)  
    {  
        int thePlayerId = otherPlayer.ActorNumber;  
        panelAttempt.SetActive(true);  
        if (thePlayerId == myGroundPlayer.myActorNumber)  
        {  
            playerName.text = myGroundPlayer.myPlayerName;  
            issueType.text = disconnectCause.ToString();  
        }  
        else  
        {  
            playerName.text = myMedolir.opponentMedolir.GetComponentInChildren<MedolirLife>().Tmp_PlayerName.text;  
            issueType.text = disconnectCause.ToString();  
        }

    }

    /// <summary>  
    /// IInRoomCallbacks.OnPlayerLeftRoom is triggered only for remote players,  
    /// Callbacks is triggered when a player leaves the room intentionally  
    /// </summary>  
    /// <param name="otherPlayer"></param>  
    public override void OnPlayerLeftRoom(Player otherPlayer)  
    {  
        base.OnPlayerLeftRoom(otherPlayer);  
        DisplayIssue(otherPlayer);

    }

    /// <summary>  
    /// -This is a PUN CALLBACKS-  
    /// Callback is called when THIS client player leaves a room after disconnection  
    /// MonoBehaviourPunCallbacks implements IConnectionCallbacks already.  
    ///The callback for disconnection is OnDisconnected(DisconnectionCause) which   
    ///now replaces all 3 callbacks from PUN1:   
    ///OnDisconnectedFromPhoton,  
    ///OnConnectionFail   
    ///OnFailedToConnectToPhoton.  
    /// </summary>  
    /// <param name="cause"></param>  
    public override void OnDisconnected(DisconnectCause cause)  
    {  
        base.OnDisconnected(cause);  
        disconnectCause = cause;  
        panelAttempt.SetActive(true);  
        playerName.text = myGroundPlayer.myPlayerName;  
        issueType.text = disconnectCause.ToString();

    }  
}

Here is the result with screenshot.
[

](https://ibb.co/6w3JDm9)

Problem : The connection issue is shown only when it's "too late"
It does display on both players that a connection issue has been detected, but unfortunately it is always too late.
I can't find a solution (both remote and local) to display the issue right away and then set a countdown before the timeOut is fired.

Clevereen
2021-01-27 16:58:36

Found the solution... I will share later

Back to top