使用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放入变量中并与其进行交互。如果没有,我如何实时知道哪个客户端已断开连接。
我知道了
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]];
}