Detect other player disconnect by quit app

Hi I just started using PUN for my unity turn base game for few weeks and I got some problem with detecting player disconnection.

Scenario:
1. Two players entered a room of max 2 players
2. One player quit application
3. Check OnPhotonPlayerDisconnected callback in the class extend PunBehaviour, the log inside is not called, use PhotonNetwork.otherPlayers to check player in the room, it show 2 players still inside

In order to let the same player rejoin the room, should the player leave room or just let the id keep in the room?
if the player need to leave room, how to let the player disconnect and leave room when he quit the application?

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    Hi @DarrenL,

    Thank you for choosing Photon!

    In order to let the same player rejoin the room, should the player leave room or just let the id keep in the room?
    Use RoomOptions.PlayerTtl != 0 and call PhotonNetwork.ReconnectAndRejoin() or PhotonNetwork.RejoinRoom(roomName);.
    if the player need to leave room, how to let the player disconnect and leave room when he quit the application?
    It's not necessary to do so, but if you want to leave asap, you can call
        private void OnApplicationQuit()
        {
            PhotonNetwork.LeaveRoom();
            PhotonNetwork.SendOutgoingCommands();
        }
    But this may not work as PUN may already be disconnected since it disconnects automatically before the app quits. In any case, the player will be disconnected from the server after a timeout. When the player is inactive (disconnects or leaves temporarily) he/she can come back before PlayerTTL milliseconds.
  • JohnTube said:

    Hi @DarrenL,

    Thank you for choosing Photon!

    In order to let the same player rejoin the room, should the player leave room or just let the id keep in the room?
    Use RoomOptions.PlayerTtl != 0 and call PhotonNetwork.ReconnectAndRejoin() or PhotonNetwork.RejoinRoom(roomName);.
    if the player need to leave room, how to let the player disconnect and leave room when he quit the application?
    It's not necessary to do so, but if you want to leave asap, you can call
        private void OnApplicationQuit()
        {
            PhotonNetwork.LeaveRoom();
            PhotonNetwork.SendOutgoingCommands();
        }
    But this may not work as PUN may already be disconnected since it disconnects automatically before the app quits. In any case, the player will be disconnected from the server after a timeout. When the player is inactive (disconnects or leaves temporarily) he/she can come back before PlayerTTL milliseconds.
    Thank you for quick reply.
    A problem I need to solve is that OnPhotonPlayerDisconnected is not called and the player count is not changed after the player quit app so that I cannot know the opponent is disconnected.
  • JohnTube
    JohnTube ✭✭✭✭✭
    Hi @DarrenL,

    A problem I need to solve is that OnPhotonPlayerDisconnected is not called and the player count is not changed after the player quit app so that I cannot know the opponent is disconnected.
    That's exactly what the code snippet I have provided tries to solve.
    Did you try it?
  • DarrenL
    DarrenL
    edited May 2019
    JohnTube said:

    Hi @DarrenL,

    A problem I need to solve is that OnPhotonPlayerDisconnected is not called and the player count is not changed after the player quit app so that I cannot know the opponent is disconnected.
    That's exactly what the code snippet I have provided tries to solve.
    Did you try it?
    Yes I just tried
    private void OnApplicationQuit()
        {
    		if(PhotonNetwork.room!=null)
    		{
    			PhotonNetwork.LeaveRoom();
    			PhotonNetwork.SendOutgoingCommands();
    		}
        }
    Cause Logerror
    Cannot send op: 254 Not connected. PeerState: Disconnected
    and
    Cannot send messages when not connected. Either connect to Photon OR use offline mode!

    At my android device, I quit app inside room.
    At my Unity editor log, PhotonNetwork.playerList.Length is still 2 and OnPhotonPlayerDisconnected is not called
  • JohnTube
    JohnTube ✭✭✭✭✭
    Hi @DarrenL,

    I invite you to read this discussion here.
  • I have a problem , on application quit was working fine if somebody quits the app , but how can we achieve same thing if clients device gets switched off all of a sudden.

  • L3sc
    L3sc
    edited June 2020
    @Jayantbarthwal
    Probably photon is disconnect after some time and you can detect it from other clients.