Unable to disconnect using the PhotonNetwork.Disconnect() function

Options
As the title states, I'm never able to disconnect from the master server using the basic PhotonNetwork.Disconnect() function. The game thinks that my game is always connected to the servers even after calling PhotonNetwork.Disconnect() (PhotonNetwork.IsConnected is always true for some reason). Anybody have any idea what the problem could be? Here is the code that's running to connect and disconnect when the main menu loads each time:
private void Start()
{
if (PhotonNetwork.IsConnected == true)
{
Debug.Log("Starting Disconnect. . .");
StartCoroutine(Disconnect());
}
else
{
PhotonNetwork.ConnectUsingSettings();
Debug.Log("Connecting. . . ");
}
}

IEnumerator Disconnect()
{
PhotonNetwork.Disconnect();
while (PhotonNetwork.IsConnected)
{
yield return null;
Debug.Log("Disconnecting. . .");
}
Debug.Log("DISCONNECTED!");
}

( It will never hit the Debug.Log("DISCONNECTED!") line of code )

I want it to disconnect when returning to the main menu after a game over so that I can manually reconnect it. I get an error when trying to join a room after completing a match ("Client is on GameServer (must be Master Server for matchmaking) and ready. Wait for callback: OnJoinedLobby or OnConnectedToMaster") and as I work around I'm trying to completely disconnect and then reconnect to the master server so I can join a lobby and then join a room again without issues. Any help is greatly appreciated!

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    edited December 2019
    Options
    Hi @Tanman1276,

    Thank you for choosing Photon!

    Not sure why your coroutine does not work, maybe the coroutine is never started in the first place, maybe there is a piece of code that automatically reconnect once disconnected and causes IsConnected to remain true always.

    Anyways, a coroutine is not the best way to go here.
    Instead you should make use of PUN Callback: IConnectionCallbacks.OnDisconnected.
    I get an error when trying to join a room after completing a match ("Client is on GameServer (must be Master Server for matchmaking) and ready. Wait for callback: OnJoinedLobby or OnConnectedToMaster")
    The proper solution to this (rematch or switch rooms) is to leave the current joined room (instead of disconnecting from all servers) and then join another one once back on master server.
    public class Example : MonoBehaviourPunCallbacks
    {
    
    private bool isLeavingRoom;
    private bool isConnecting:
    
    void Start()
    {
          if (PhotonNetwork.IsConnected)
          {
              isConnecting = PhotonNetwork.ConnectUsingSettings();
          }
    }
    
    void LeaveCurrentRoom()
    {
          if (PhotonNetwork.InRoom)
          {
              leftRoom = PhotonNetwork.LeaveRoom();
           }
    }
    
    
    
    public override void OnConnectedToMaster()
    {
         if (isConnecting) 
         {
              isConnecting = false;
              // join lobby, create a room or join a room
         }
         else if (isLeavingRoom)
         {
              isLeavingRoom = false;
              // join lobby, create a room or join a room
         }
    }
    
    public override void OnDisconnected(DisconnectCause cause)
    {
              isConnecting = false;
              isLeavingRoom = false;
    }
    
  • Unfortunately the above logic is not working for me. It says after I load the matchmaking scene after my game is over

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

    UnityEngine.Debug:LogWarning (object)


    Could you tell me where exactly isLeavingRoom should be set to true? I set it in OnLeftRoom().Please correct me if I am wrong

    Also could you please tell what should be the initial values to isLeavingRoom and isConnecting should be.

    I am just somehow not able to understand the above code logic. If you could tell me in that would be awesome. I am facing a similar problem and raised it here as well as here . Please help me I am stuck in this problem for past 1 day