为什么Rachet推送服务器(PHP)没有响应?

时间:2018-10-31 22:58:16

标签: php zeromq ratchet reactphp

我正在尝试关注:Ratchet Tutorial - Push to an existing site

我的pushserver.php

<?php

require_once("../react_api/includes.php");

use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Ratchet\Wamp\WampServer;
use Ratchet\Server\IoServer;
use React\EventLoop\Factory;
use React\ZMQ\Context;
use Models\SCSTRealtimeSubsObject;

// The event loop that will keep on triggering
$loop   = Factory::create();

// Our custom pusher that will do the logic, $loop is optional
$pusher =  new SCSTRealtimeSubsObject();

// Listen for the web server to make a ZeroMQ push after an ajax request
$context    = new Context($loop);

$pull       = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->setSockOpt(ZMQ::SOCKOPT_HWM, 0);
$pull->bind("tcp://127.0.0.1:5555"); //Binding to itself means the client can only connect to itself
$pull->on('error', function ($e) { 
    echo $e->getMessage();
 });

//On a 'message' event, pass the data to the myMessageHandler method of the MyPusherClass
$pull->on('message', array($pusher, 'onSend'));

echo "Realtime server now listening on localhost@5555 Binded to ".RT_SERVER_PORT."\n";
// Set up our WebSocket server for clients wanting real-time updates
$webSock   = new React\Socket\Server('0.0.0.0:8888', $loop); // Binding to 0.0.0.0 means remotes can connect
$webServer = new IoServer(
    new HttpServer(
        new WsServer(
            new WampServer(
                $pusher
            )
        )
    )
    ,
    $webSock
);

$loop->run();

?>

我的推动器如下:

<?php

namespace Models;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\WampServerInterface;

class SCSTRealtimeSubsObject implements WampServerInterface {
    protected $subscriber_topics = [];
    public function __construct() {
        echo "Server Instantiated...\n";
        $this->clients = new \SplObjectStorage;
    }
    public function onSend($msg){ // Message from the onMessage
        echo "Message has been sent!";
    }
    public function onOpen(ConnectionInterface $conn) {
        echo "A new connection has been established!";
    }
    public function onSubscribe(ConnectionInterface $conn, $topic) {
        echo "Someone subscribe!"
    }
    public function onUnSubscribe(ConnectionInterface $conn, $topic) {
        echo "On unsubscribe triggered ...";
    }
    public function onClose(ConnectionInterface $conn) {
        echo "Connection {$conn->resourceId} has disconnected... \n";
    }
    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "Server error: ". $e->getMessage();
    }
    public function onCall(ConnectionInterface $conn, $id, $topic, array $params) {
        echo "On call called...";
    }
    public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible) {
        echo "Closing onPublish has triggered ...\n";
        $conn->close();
    }
}

?>

请注意,我的推入器类在每个方法中都有“ echo”。所以我像在命令行“ php pushserver.php”中那样运行服务器

然后我创建一个像这样的脚本(test.php)

$loop   = \React\EventLoop\Factory::create();
$context = new \React\ZMQ\Context($loop);
$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
$socket->connect("tcp://localhost:5555");
$socket->send("FOO");

然后尝试通过从另一个命令行窗口运行上述脚本并键入“ php test.php”来测试是否从pushserver.php得到响应

我至少应该得到一个响应,说明已建立连接或已发送消息或其他东西。

但是我什么都没得到。我顺便说一句在我的Windows机器上运行它。我检查了所有可能阻止任何连接的防火墙设置。我的两个脚本也都在2个不同的命令行窗口中运行。

我在这里的理解不正确吗?为什么我的“ test.php”根本无法发送或连接到我的pushserver?

0 个答案:

没有答案