How to disconnect and reconnect/rejoin in room
The whole answer can be found below.
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).
How to disconnect and reconnect/rejoin in room
Gagan
2022-10-28 09:43:09
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.
Comments
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 ;)
DarkLouis 2022-10-30T18:20:22+00:00
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 ;)
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.
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.
Back to top