Получить идентификатор ресурса клиента клиента, который отключается в храповик php в режиме реального времени - PullRequest
1 голос
/ 01 мая 2020

Код с использованием:

protected $clients;

public function __construct() {
    $this->clients = new \SplObjectStorage;
}

public function onOpen(ConnectionInterface $conn) {
    $this->clients->attach($conn);

}

public function onClose(ConnectionInterface $conn) {
    $this->clients->detach($conn);

}

Когда клиент отключается, запускается функция Onclose, есть ли способ, которым я могу поместить идентификатор ресурса клиента в переменную и взаимодействовать с ним. Если нет, то как бы я сказал, какой клиент отключен в режиме реального времени.

1 Ответ

0 голосов
/ 01 мая 2020

Я понял это

public function onClose(ConnectionInterface $conn) {
    $a = array();
    $b = array();

    //Gets all the client Ids
    foreach($this->clients as $client){
        array_push($a,$client->resourceId);
    }

    //Deletes the disconnected client
    $this->clients->detach($conn);

    //Gets all the new client Ids
    foreach($this->clients as $client){
        array_push($b,$client->resourceId);
    }

    //array is made that includes the disconnceted client id by comparing both arrays made earlier
    $closedClientArray = array_diff($a,$b);

    //Client id is extracted from array and put in variable as a string
    $closeClientString = $closedClientArray[array_keys($closedClientArray)[0]];

    } 
...