Need a way to kick inactive player

Options
Hi, I'm developing game on android with unity using pun+.
I get a lot of users who connects to a room then puts the app to background, so that the game becomes inactive while connected to photon. when this happens, they would stay in the room unless they close the app. I need a way to kick those player from the room.

I've tried using PhotonNetwork.CloseConnection(player); to kick them as room master, but it doesn't seem to work. I'm assuming it's because their game is on the background, so they cannot receive the event sent by photon to tell them to disconnect. if this is the case, I will need a different way to kick those players from the room. Is there any other way around this?

thank you in advance.

Best Answer

Answers

  • JohnTube
    JohnTube ✭✭✭✭✭
    edited August 2015
    Options
    Hi @potte,

    Did you try creating Rooms with PlayerTTL = 0 ? That way actors can never become inactive : they will be removed from the ActorList of the Room as soon as they disconnect.

    I'm assuming this can be done using PUN as I'm more familiar with LoadBalancing SDK / TurnBased SDK.

    If you want to avoid "kicking" players due to unexpected disconnects, try to change the value to something like this
    0 < PlayerTTL < EmptyRoomTTL
    I hope this condition is the correct workaround.
  • N1warhead
    Options
    yeah like Tobias said, you can do like like
    public float inActiveKick;
    public float resetKickTime;

    void Update(){
    if(!Input.anyKey){
    inActiveKick -= Time.deltaTime;
    if(inActiveKick <= 0){
    PhotonNetwork.Disconnect();
    }
    }

    if(Input.AnyKey){
    inActiveKick = resetKickTime;
    }

    }

    SOmething like that should work.
  • potte
    Options
    Tobias, it seems your answer is the general solution i get from all similar question on the forum. I've hear this before and tried to do PhotonNetwork.Disconnect() on the client side, but without success. N1warhead's code won't work because when the unityplayer is in the background, it pauses and its update function won't get called.

    i've also tried using,
    void OnApplicationFocus(bool isFocused) {
    if (!isFocused) {
    Debug.Log("not focused");
    //try disconnect from photon
    if (PhotonNetwork.room != null) {
    Debug.Log("leave room");
    PhotonNetwork.LeaveRoom();
    }
    }
    else{
    Debug.Log("regain focused");
    }
    }
    but this would only work on eidtor, when I build the android version, OnApplicationFocus(bool isFocused) won't get call when user put it to background, it'll only get call when user re-enter the app. basically calls OnApplicationFocus twice, once for unfocus and second time for the re-enter.
  • potte
    Options
    Actually it works out fine after replacing PhotonNetwork.LeaveRoom(); with PhotonNetwork.Disconnect(), I was assuming the result would be the same, but apparently they are different.
  • Tobias
    Options
    They are different. One leaves the room and the other closes the connection. It also removes that player from the room (unless the room is a Turnbased one where players can be inactive) but the connection is closed.
    Also, Disconnect sends that message immediately. In that same method call, a package is sent on it's way to the server. It's not guaranteed to arrive but it has a high probability. If this single "disconnect" message gets lost, the server will timeout the client, because it doesn't react. In worst case, the client is considered online for 10 seconds after this.