How to disconnect and reconnect/rejoin in room
As a requirement, when the app goes in the background makes the user exit from the room and if the app comes in the foreground makes the user rejoin the room.
////For leave/////
OnApplicationPause()
PhotonNetwork.LeaveRoom();
PhotonNetwork.Disconnect();
////// For rejoin ////
OnApplicationFocus()
PhotonNetwork.ConnectUsingSettings("0.5");
if (!string.IsNullOrEmpty(this.previousRoom))
{
Debug.Log("ReJoining previous room: " + this.previousRoom); PhotonNetwork.ReJoinRoom(this.previousRoom);
}
When the app comes in the foreground the user is not rejoining.
been trying this quite a long time, @photon any help will be appreciated.
Answers
-
Try to store the name of the room when you join (like in "previousRoom").
And when your application comes in the foreground you just do :
PhotonNetwork.JoinRoom(previousRoom)
So it will be that :
////When you join/////
string roomToJoin = "abc"
PhotonNetwork.JoinRoom(roomToJoin );
////For leave/////
OnApplicationPause()
PhotonNetwork.LeaveRoom();
PhotonNetwork.Disconnect();
////// For rejoin ////
OnApplicationFocus()
PhotonNetwork.ConnectUsingSettings("0.5");
PhotonNetwork.JoinRoom(roomToJoin );
I wish it gonna be helpfull for you ;)
0 -
this.previousRoom is having previous room value like this
this.previousRoom = PlayerPrefs.GetString(previousRoomPlayerPrefKey);
and getting error
ReJoinRoom failed. Client is not on Master Server or not yet ready to call operations. Wait for callback: OnJoinedLobby or OnConnectedToMaster.
0 -
Oh I think I know.
When you use Photon.Disconnect(), you disconnect from the master too.
So you need to reconnect the player to the master.
0