Kick out all players or close the room when the master client is disconnected
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).
Kick out all players or close the room when the master client is disconnected
YannickAtTuring
2020-07-20 15:50:49
Hi,
we're trying to make a product with a strict hierarchy in which the master client has special rights that the other users don't have. Because of this, we want to prevent PUN's default behaviour of switching the master client if the current master client leaves and instead close the room or force every other client in the room to leave as well and return to the lobby scene.
Is there a way to facilitate this? Are there callbacks I can make use of?
Thank you for your time!
Comments
OneManArmy
2020-07-22 11:24:09
Try PhotonNetwork.LeaveRoom() on OnMasterClientSwitched()
Hi @YannickAtTuring,
Thank you for choosing Photon!
The master client can try to do this before disconnecting (in OnApplicationPause or OnApplicationQuit):
close the room PhotonNetwork.CurrentRoom.IsOpen = false (also if you change these values CurrentRoom.EmptyRoomTtl = 0, CurrentRoom.PlayerTtl = 0) & send PhotonNetwork.CloseConnection(player) to all other joined actors. Don't forget to send outgoing commands right away (see here).
The other already joined actors can do as @OneManArmy suggested.
If the room is not closed future actors may join so detect if original/initial master client (room creator) is there or not: MasterClient.ActorNumber != 1 and then leave the room.
YannickAtTuring
2020-07-23 14:03:12
Thank you @JohnTube - this already helps a lot. I didn't know about the ability to send right away. Very useful!
As far as I understand, your solution covers cases in which the master client switches off the device or closes the software. Would this also cover cases in which the master client loses their internet connection? Should I just bet on OnMasterClientSwitched() for edge cases like this?
Thanks again, I learned a lot from your reply.
Would this also cover cases in which the master client loses their internet connection? Should I just bet on OnMasterClientSwitched() for edge cases like this?
Yes but this does not handle one edge case where the master client is not responsive and the server does not notice this for 10 seconds (timeout period). The only way to handle this is to implement a custom keep alive or heart beat logic in your game. You can search the forum to find out more about it.
@JohnTube wrote: »
Hi @YannickAtTuring,
Thank you for choosing Photon!
The master client can try to do this before disconnecting (in OnApplicationPause or OnApplicationQuit):
close the room PhotonNetwork.CurrentRoom.IsOpen = false (also if you change these values CurrentRoom.EmptyRoomTtl = 0, CurrentRoom.PlayerTtl = 0) & send PhotonNetwork.CloseConnection(player) to all other joined actors. Don't forget to send outgoing commands right away (see here).The other already joined actors can do as @OneManArmy suggested.
If the room is not closed future actors may join so detect if original/initial master client (room creator) is there or not: MasterClient.ActorNumber != 1 and then leave the room.
Hi John! I have one (I hope not so silly) question about CloseConnection() that I have not been able to answer reading the docs: when a Master Client uses CloseConnection to kick a player does this disconnect the player also from server (so they need to connect again)? Or does that just kicks them from the room?
Thanks!
Hi @Vazkuz,
Thank you for choosing Photon!
CloseConnection event sent by MasterClient will cause players to leave the room (PhotonNetwork.LeaveRoom(false)).
Back to top