WebSocket::disconnect PHP Method

disconnect() public method

public disconnect ( $socket )
    function disconnect($socket)
    {
        $found = null;
        $n = count($this->users);
        for ($i = 0; $i < $n; $i++) {
            if ($this->users[$i]->socket == $socket) {
                $found = $i;
                break;
            }
        }
        if (!is_null($found)) {
            array_splice($this->users, $found, 1);
        }
        $index = array_search($socket, $this->sockets);
        socket_close($socket);
        $this->log($socket . " DISCONNECTED!");
        if ($index >= 0) {
            array_splice($this->sockets, $index, 1);
        }
    }

Usage Example

Example #1
0
 public function disconnect($socket)
 {
     $id = $this->players->delPlayer($socket);
     // a player disconnected so send to all
     $this->sendToAll($socket, Commands::PLAYER_DISCONNECT . $this->commandee->setPlayer($id));
     $map_id = $this->maps->getMapByPlayerId($id);
     if ($map_id !== NULL) {
         $map_id = $map_id->getId();
         // the map's other player state goes to free and send him to a game over message
         $other_player = $this->maps->getMap($map_id)->getOtherPlayer($id);
         $this->players->getPlayer($other_player)->setState(\Entity\States::FREE);
         $this->sendToOne($this->players->getSocketById($other_player), Commands::GAME_OVER . Commandee::NO_ID);
         $this->maps->delMap($map_id);
     }
     parent::disconnect($socket);
 }