Почему нет ответа от Rachet push server (PHP)? - PullRequest
0 голосов
/ 01 ноября 2018

Я пытаюсь следовать: Учебное пособие по трещотке - Нажмите на существующий сайт

Мой 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");

Затем попробуйте проверить, получаю ли я ответ от своего pushserver.php, запустив вышеуказанный скрипт из другого окна командной строки и набрав «php test.php»

Я должен, по крайней мере, получить ответ о том, что установлено соединение или сообщение отправлено или что-то в этом роде.

Но я ничего не понимаю. Кстати, я запускаю это на своей машине с Windows. Я проверил все настройки брандмауэра, которые могут блокировать любое соединение. Оба сценария я запускал в двух разных окнах командной строки.

Мое понимание здесь неверно? Почему мой "test.php" вообще не может отправлять или подключаться к моему push-серверу?

...