如何获取特定用户的连接对象? [英] how to get the connection object of a specific user?

查看:26
本文介绍了如何获取特定用户的连接对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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.

这篇关于如何获取特定用户的连接对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆