clients = new \SplObjectStorage; } public function onOpen(ConnectionInterface $conn) { // Store the new connection $this->clients->attach($conn); echo "New connection! ({$conn->resourceId})\n"; } public function onMessage(ConnectionInterface $from, $msg) { foreach ($this->clients as $client) { if ($from !== $client) { // Send the message to all connected clients except the sender $client->send($msg); } } } public function onClose(ConnectionInterface $conn) { // Remove the connection $this->clients->detach($conn); echo "Connection {$conn->resourceId} has disconnected\n"; } public function onError(ConnectionInterface $conn, \Exception $e) { echo "An error has occurred: {$e->getMessage()}\n"; $conn->close(); } } ?>