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 = 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(); - <?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(); - <?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(); - <?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.php , он просто зависает. Я понятия не имею, почему, я предполагаю, что где-то в очереди он не говорит другой стороне, что он получил данные и, следовательно, не может отправить другой запрос.

Кроме того, мой $pull->on('message', array($pusher, 'onPull')); в настоящее время отправляет данные в мой onPull() в моем классе Pusher. На данный момент я просто ничего не возвращаю, но если я верну некоторые данные, как мне вернуть это запрос?

Вы должны добавить код из своего класса Pusher чтобы мы могли видеть метод onPull … похоже, что он по-прежнему является главным образом вашим кодом push/pull а не req/rep , что может отбросить некоторые вещи (для справки другого, его первого вопрос, который включал некоторые признаки его использования push/pull , здесь )

Однако, я думаю, что я вижу проблему (комментарии в строке):

 <?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(); - <?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(); - <?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(); - <?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();