how to get the connection object of a specific user?(如何获取特定用户的连接对象?)
问题描述
我正在使用 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.
这篇关于如何获取特定用户的连接对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何获取特定用户的连接对象?
基础教程推荐
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何替换eregi() 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
