我的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 = React\EventLoop\Factory::create();
$pusher = new MyApp\Pusher;
$context = new React\ZMQ\Context($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 React\Socket\Server($loop);
$webSock->listen(8080, '0.0.0.0');
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
$pusher
)
),
$webSock
);
$loop->run();
因此,当我的服务器脚本运行时,我会运行pull.php
。这有效,并将response
文字发回给我var_dump
。但是,如果我然后重新加载我的pull.php
脚本,它就会挂起。我不知道为什么,我在这里假设某个地方没有告诉对方它已收到数据,因此无法发送另一个请求。
此外,我的$pull->on('message', array($pusher, 'onPull'));
当前将数据发送到我的Pusher类中的onPull()
方法。目前我什么都不返回,但如果我返回一些数据,我该如何将其返回给请求者?
答案 0 :(得分:1)
您应该添加Pusher
课程中的代码,以便我们看到onPull
方法...这看起来仍然主要是您的push/pull
代码而不是req/rep
},这可能会抛出一些东西(为了其他人的参考,他的第一个问题包括他使用push/pull
的一些迹象,是here)
但是,我认为我看到了这个问题(在线评论):
<?php
require './vendor/autoload.php';
$loop = React\EventLoop\Factory::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 MyApp\Pusher;
$context = new React\ZMQ\Context($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 React\Socket\Server($loop);
$webSock->listen(8080, '0.0.0.0');
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
$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();
我认为你根本不想要Ratchet或Websockets,你只想与ZMQ客户端交谈,对吗?所以,拉出那个代码。您最想要的是定义您的on-message
处理程序来处理请求并发送响应。请尝试以下操作(这是基于对React
的一项非常少的研究,我找不到req/rep
示例,所以如果这不起作用,请告诉我们)
<?php
require './vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$context = new React\ZMQ\Context($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();