php - how to get the connection object of a specific user? -


i working in real time symfony app using ratchet library, in app need send data specific user logic solution use sessionprovider attach symfony2 session object each incoming connection object. documentation states have setup non-native session handler store sessions i.e. in database via pdo. , work fine moment need connection object of specific user send him data in other way need find connection object reference user , can't find way ? her's server code :

    $app=new aggregateapplication();     $loop   = \react\eventloop\factory::create();     $context = new \react\zmq\context($loop);     $pull = $context->getsocket(\zmq::socket_pull);     $pull->bind('tcp://127.0.0.1:5555');     $pull->on('message', array($app, 'onnotification'));     $websock = new \react\socket\server($loop);     $websock->listen(8080, '127.0.0.1');     $handler = $this->getcontainer()->get('session.handler');     $server=new \ratchet\wamp\wampserver($app);     $server = new sessionprovider($server, $handler);     $webserver = new \ratchet\server\ioserver(new \ratchet\websocket\wsserver($server),$websock);     $loop->run(); 

i had exact same question myself (minus symfony) , here did.

based on hello world tutorial, have substituted splobjectstorage array. before presenting modifications, i'd comment if followed through tutorial , understood it, thing prevented arriving @ solution not knowing splobjectstorage is.

class chat implements messagecomponentinterface {     protected $clients;      public function __construct() {         $this->clients = array();     }      public function onopen(connectioninterface $conn) {         // store new connection send messages 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" %d other connection%s' . "\n"             , $from->resourceid, $msg, $numrecv, $numrecv == 1 ? '' : 's');          foreach ($this->clients $key => $client) {             if ($from !== $client) {                 // sender not receiver, send each client connected                 $client->send($msg);             }         }         // send message known resourceid (in example sender)         $client = $this->clients[$from->resourceid];         $client->send("message sent $numrecv users.");     }      public function onclose(connectioninterface $conn) {         // connection closed, remove it, can no longer send 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 make useful may want add in db connection, , store/retrieve resourceids.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -