获取在棘轮php中实时断开连接的客户端的客户端资源ID

问题描述 投票:1回答:1

使用im的代码:

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函数,有没有一种方法可以将客户端资源ID放入变量中并与其进行交互。如果没有,我如何实时知道哪个客户端已断开连接。

php websocket client ratchet
1个回答
0
投票

我知道了

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]];

    } 
© www.soinside.com 2019 - 2024. All rights reserved.