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