Greetings to the people of stackoverflow, in recent days I have been looking at websites and a PHP library called Ratchet (which is perfect for writing server applications on web servers in PHP). In Ratchet's official docs, they recommend using SplObjectStorage (which I never heard of) to manage client connection objects.
In most server applications, you probably need to store some data about each client (for example, in my case, when I experiment with writing a simple messaging server, I need to store the data as a click alias and possibly something more), so how I understand, I can add a client object and an array with client data to SplObjectStorage when a new connection is opened, as shown below.
public function onOpen(ConnectionInterface $conn) {
$this->clients[$conn] = array('id' => $conn->resourceId, 'nickname' => '');
}
However, I'm not sure if the best way to get an object from SplObjectStorage is by value in the data array (for example, by the alias of users), one way to do this would be as follows:
foreach($this->clients as $client){
$data = $this->clients->offsetGet($client);
if($data['nickname'] == $NickNameIAmLookingFor){
}
}
But I believe that there is a better way to do this, so any advice would be greatly appreciated.
Thanks in advance.
Binni source
share