gpt4 book ai didi

php - 实时获取 Ratchet php中断开连接的客户端的客户端资源ID

转载 作者:行者123 更新时间:2023-12-04 16:13:39 25 4
gpt4 key购买 nike

代码即时使用:

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

}

关于php - 实时获取 Ratchet php中断开连接的客户端的客户端资源ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61536413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com