問題描述
我正在使用 Ratchet 庫的實時 Symfony 應用程序中工作,在此應用程序中,我需要向特定用戶發送一些數據,因此邏輯解決方案是使用SessionProvider 將一個 Symfony2 Session 對象附加到每個傳入的 Connection 對象.正如文檔所述,我已經設置了一個非本地會話處理程序來存儲我的會話,即通過 PDO 存儲在數據庫中.并且目前工作正常,但我需要獲取特定用戶的 Connection 對象以向他發送一些數據,因此以其他方式我需要找到引用該用戶的連接對象,但我找不到方法它 ?她是我的服務器代碼:
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 solution was to use the SessionProvider that will attach a Symfony2 Session object to each incoming Connection object. As the documentation states I have setup a non-native session handler to store my sessions i.e. in a database via PDO. and that work fine for the moment but I need to get the Connection object of a specific user to send him some data so in other way I need to find the connection object that reference to this user and I can't find a way to do it ? her's my server code :
$app=new AggregateApplication();
$loop = ReactEventLoopFactory::create();
$context = new ReactMQContext($loop);
$pull = $context->getSocket(MQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555');
$pull->on('message', array($app, 'onNotification'));
$webSock = new ReactSocketServer($loop);
$webSock->listen(8080, '127.0.0.1');
$handler = $this->getContainer()->get('session.handler');
$server=new RatchetWampWampServer($app);
$server = new SessionProvider($server, $handler);
$webServer = new RatchetServerIoServer(new RatchetWebSocketWsServer($server),$webSock);
$loop->run();
推薦答案
我自己也有同樣的問題(除了 Symfony),這就是我所做的.
I had the exact same question myself (minus Symfony) and here is what I did.
根據 hello world 教程,我用數組替換了 SplObjectStorage.在介紹我的修改之前,我想評論一下,如果您遵循該教程并理解了它,那么阻止您自己獲得此解決方案的唯一原因可能是不知道 SplObjectStorage 是.
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})
";
}
public function onMessage(ConnectionInterface $from, $msg) {
$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "
"
, $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
";
}
public function onError(ConnectionInterface $conn, Exception $e) {
echo "An error has occurred: {$e->getMessage()}
";
$conn->close();
}
}
當然,為了讓它真正有用,您可能還想添加一個數據庫連接,并存儲/檢索這些資源 ID.
Of course to make it really useful you may also want to add in a DB connection, and store/retrieve those resourceIds.
這篇關于如何獲取特定用戶的連接對象?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!