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! Your search result can be found below. Plus, we offer support via 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.

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()

JohnTube
2020-07-22 13:49:38

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.

JohnTube
2020-07-23 14:21:08

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.

Vazkuz
2021-02-10 19:19:39

@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!

JohnTube
2021-02-10 22:31:16

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