ZMQ REP REQ -发送接收不工作



我已经启动并运行了我的websocket服务器,并且为了我自己的使用,我想在执行静态PHP脚本时获得连接的列表。

我的PHP脚本pull.php:

$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_REQ);
$socket->connect("tcp://localhost:5552");
$socket->send('data');
$test = $socket->recv();
var_dump($test);

然后在我的服务器脚本:

<?php
require './vendor/autoload.php';
$loop   = ReactEventLoopFactory::create();
$pusher = new MyAppPusher;
$context = new ReactZMQContext($loop);
$pull = $context->getSocket(ZMQ::SOCKET_REP);
$pull->bind('tcp://127.0.0.1:5552');
$pull->on('message', array($pusher, 'onPull'));
$pull->recv();
$pull->send('response');
$webSock = new ReactSocketServer($loop);
$webSock->listen(8080, '0.0.0.0');
$webServer = new RatchetServerIoServer(
    new RatchetHttpHttpServer(
        new RatchetWebSocketWsServer(
            $pusher
        )
    ),
    $webSock
);
$loop->run();

所以当我的服务器脚本运行时,我运行pull.php。这是有效的,并将我var_dump输出的response文本发送回我。然而,如果我然后去重新加载我的pull.php脚本,它只是挂起。我不知道为什么,我在这里假设它没有告诉另一方它已经收到数据,因此不能发送另一个请求。

同样,我的$pull->on('message', array($pusher, 'onPull'));当前将数据发送到我的push类中的onPull()方法中。目前我什么都不返回,但是如果我返回一些数据,我如何将这些数据返回给请求者呢?

您应该添加来自Pusher类的代码,以便我们可以看到onPull方法…这看起来仍然主要是你的push/pull代码,而不是req/rep,这可能会抛出一些东西(为了其他人的参考,他的第一个问题,其中包括他使用push/pull的一些指示,在这里)

然而,我想我看到了问题(注释内嵌):

<?php
require './vendor/autoload.php';
$loop   = ReactEventLoopFactory::create();
/*******************
first red flag - you use your Pusher class, which presumably is set up
to handle a push/pull scenario rather than a req/rep scenario
*******************/
$pusher = new MyAppPusher;
$context = new ReactZMQContext($loop);
/*******************
Why are you naming a REP socket $pull?  Change your code appropriately when you
copy/paste, it will help issues to stand out more
*******************/
$pull = $context->getSocket(ZMQ::SOCKET_REP);
$pull->bind('tcp://127.0.0.1:5552');
/*******************
second red flag - you set up a message handler that deals with a message event
and then you go on to manually call recv and send - this is probably your issue.
recv() will block until it receives your first request from the client, then it
will send your 'response', but this will only ever handle the very first message    
*******************/
$pull->on('message', array($pusher, 'onPull'));
$pull->recv();
$pull->send('response');
/*******************
I don't know what this is doing here, since your client is a ZMQ client and not an
HTTP client
*******************/
$webSock = new ReactSocketServer($loop);
$webSock->listen(8080, '0.0.0.0');
$webServer = new RatchetServerIoServer(
    new RatchetHttpHttpServer(
        new RatchetWebSocketWsServer(
            $pusher
        )
    ),
    $webSock
);
/*******************
presumably this loop should handle subsequent messages, but it's either set up
for push/pull or HTTP, or both, I can't quite tell
*******************/
$loop->run();

我不认为你想要棘轮或Websockets在这里,你只是想与一个ZMQ客户端,对吗?把代码拉出来。您最有可能想要的是定义on-message处理程序来处理请求并发送响应。尝试以下(这是基于对React的很少研究,在那里我找不到req/rep的例子,所以如果这不起作用,然后让我知道)

<?php
require './vendor/autoload.php';
$loop   = ReactEventLoopFactory::create();
$context = new ReactZMQContext($loop);
$rep = $context->getSocket(ZMQ::SOCKET_REP);
$rep->bind('tcp://127.0.0.1:5552');
$rep->on('message', function($msg) use ($rep) {
    // the "use ($rep)" syntax is PHP 5.4 or later
    // if you're using an older version of PHP, just use $GLOBAL['rep'] instead
    $rep->send('response');
});
$loop->run();

最新更新