1

我已经启动并运行了我的 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'));目前将数据发送到我onPull()的 Pusher 类中的方法中。目前我什么也没返回,但是如果我返回一些数据,我该如何将其返回给请求者?

4

1 回答 1

1

您应该从您的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();

我认为您根本不想要 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();
于 2014-05-29T14:43:58.687 回答