how to get the connection object of a specific user?

后端 未结 2 2084
隐瞒了意图╮
隐瞒了意图╮ 2020-12-15 11:48

I am working in a real time Symfony app using Ratchet library, in this app I need to send some data to a specific user so the logic solutio

相关标签:
2条回答
  • 2020-12-15 11:59

    I had the exact same question myself (minus Symfony) and here is what I did.

    Based on the hello world tutorial, I have substituted SplObjectStorage with an array. Before presenting my modifications, I'd like to comment that if you followed through that tutorial and understood it, the only thing that prevented you from arriving at this solution yourself is probably not knowing what SplObjectStorage is.

    class Chat implements MessageComponentInterface {
        protected $clients;
    
        public function __construct() {
            $this->clients = array();
        }
    
        public function onOpen(ConnectionInterface $conn) {
            // Store the new connection to send messages to later
            $this->clients[$conn->resourceId] = $conn;
            echo "New connection! ({$conn->resourceId})\n";
        }
    
        public function onMessage(ConnectionInterface $from, $msg) {
            $numRecv = count($this->clients) - 1;
            echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
                , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
    
            foreach ($this->clients as $key => $client) {
                if ($from !== $client) {
                    // The sender is not the receiver, send to each client connected
                    $client->send($msg);
                }
            }
            // Send a message to a known resourceId (in this example the sender)
            $client = $this->clients[$from->resourceId];
            $client->send("Message successfully sent to $numRecv users.");
        }
    
        public function onClose(ConnectionInterface $conn) {
            // The connection is closed, remove it, as we can no longer send it messages
            unset($this->clients[$conn->resourceId]);
    
            echo "Connection {$conn->resourceId} has disconnected\n";
        }
    
        public function onError(ConnectionInterface $conn, \Exception $e) {
            echo "An error has occurred: {$e->getMessage()}\n";
    
            $conn->close();
        }
    }
    

    Of course to make it really useful you may also want to add in a DB connection, and store/retrieve those resourceIds.

    0 讨论(0)
  • 2020-12-15 12:03

    this is what I did, has some improvements on the same idea.

    adds 2 functions that you can call elsewhere: send_to() and multicast().

    namespace mine;
    use Ratchet\MessageComponentInterface;
    use Ratchet\ConnectionInterface;
    
    class ws implements MessageComponentInterface {
        protected $clients;
        protected $clientids;
    
        public function __construct() {
            $this->clients = new \SplObjectStorage; 
            $this->clientids = array();
        }
    
        public function multicast($msg) {
            foreach ($this->clients as $client) $client->send($msg);
        }
    
        public function send_to($to,$msg) {
            if (array_key_exists($to, $this->clientids)) $this->clientids[$to]->send($msg);
        }
    
        public function onOpen(ConnectionInterface $conn) {
            $socket_name = "{$conn->resourceId}@{$conn->WebSocket->request->getHeader('X-Forwarded-For')}";
            $this->clients->attach($conn,$socket_name);
            $this->clientids[$socket_name] = $conn;
        }
    
        public function onMessage(ConnectionInterface $from, $msg) {
            $this->multicast($msg);
        }
    
        public function onClose(ConnectionInterface $conn) {
            unset($this->clientids[$this->clients[$conn]]);
            $this->clients->detach($conn);
        }
    
        public function onError(ConnectionInterface $conn, \Exception $e) {
            $conn->close();
        }
    }
    
    0 讨论(0)
提交回复
热议问题