ZMQ REP REQ - 发送接收不起作用 [英] ZMQ REP REQ - Send Receive not working

查看:54
本文介绍了ZMQ REP REQ - 发送接收不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have my websocket server up and running, and for my own use I want to get back a list of the connected when I execute a static PHP script.

我的 PHP 脚本 pull.php:

My PHP script 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);

然后在我的服务器脚本上:

Then on my server script:

<?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.这有效,并将我 var_dump 输出的 response 文本发回给我.但是,如果我然后去重新加载我的 pull.php 脚本,它就会挂起.我不知道为什么,我在这里假设在某个地方它没有告诉对方它已经收到了数据,因此无法发送另一个请求.

So when my server script is running, I then run pull.php. This works, and sends me back the response text which I var_dump out. However, if I then go and reload my pull.php script, it just hangs. I've no idea why, I am assuming here that somewhere down the line it's not telling the other party that it has received the data and thus can't send another request.

此外,我的 $pull->on('message', array($pusher, 'onPull')); 当前将数据发送到我的 onPull() 我的 Pusher 类中的方法.目前我什么都不返回,但如果我返回一些数据,我如何将其返回给请求者?

Also, my $pull->on('message', array($pusher, 'onPull')); currently sends the data into my onPull() method in my Pusher class. At the moment I just return nothing, but if I return some data how do I get this back to the requester?

推荐答案

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

You should add the code from your Pusher class so we can see the onPull method... this looks like it is still mostly your push/pull code rather than req/rep, which may throw some things off (for other's reference, his first question which included some indication of his use of push/pull, is here)

但是,我想我看到了这个问题(在线评论):

However, I think I see the issue (comments in-line):

<?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 示例,所以如果这不起作用,那么告诉我)

I don't think you want Ratchet or Websockets at all here, you just want to talk to a ZMQ client, correct? So, pull that code out. What you most likely want instead is to define your on-message handler to handle the request and send a response. Try the following (this is based off of a very little research into React, where I couldn't find a req/rep example, so if this doesn't work then let me know)

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

这篇关于ZMQ REP REQ - 发送接收不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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