Ratchet Push Server Учебное пособие - PullRequest
0 голосов
/ 16 ноября 2018

Я пытаюсь заставить учебник с трещоткой работать.

http://socketo.me/docs/push

Я делаю именно то, что говорится в учебнике, но мой подписчик не получит никаких сообщений.

Мой server.php

<?php
require dirname(__DIR__) . '/vendor/autoload.php';

$loop   = React\EventLoop\Factory::create();
$pusher = new MyApp\Pusher;

// Listen for the web server to make a ZeroMQ push after an ajax request
$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on('message', array($pusher, 'onBlogEntry'));

// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server('0.0.0.0:8080', $loop); // Binding to 0.0.0.0 means remotes can connect
$webServer = new Ratchet\Server\IoServer(
    new Ratchet\Http\HttpServer(
        new Ratchet\WebSocket\WsServer(
            new Ratchet\Wamp\WampServer(
                $pusher
            )
        )
    ),
    $webSock
);

$loop->run();

Мой addblog.php

<?php
// post.php ???
// This all was here before  ;)
$entryData = array(
    'category' => 'kittensCategory'
  , 'title'    => 'Test'
  , 'article'  => 'Test'
  , 'when'     => time()
);

$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
$socket->connect("tcp://localhost:5555");

$socket->send(json_encode($entryData));

И мой listener.html

    <script src="autobahn.js"></script>
<script>
    var conn = new ab.Session('ws://localhost:8080',
        function() {
            conn.subscribe('kittensCategory', function(topic, data) {
                // This is where you would add the new article to the DOM (beyond the scope of this tutorial)
                console.log('New article published to category "' + topic + '" : ' + data.title);
            });
        },
        function() {
            console.warn('WebSocket connection closed');
        },
        {'skipSubprotocolCheck': true}
    );
</script>

И мой Pusher.php

<?php
namespace MyApp;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\WampServerInterface;

class Pusher implements WampServerInterface {
/**
 * A lookup of all the topics clients have subscribed to
 */
protected $subscribedTopics = array();

public function onSubscribe(ConnectionInterface $conn, $topic) {
  echo "Hello to: ".$topic;
    $this->subscribedTopics[$topic->getId()] = $topic;
}

/**
 * @param string JSON'ified string we'll receive from ZeroMQ
 */
public function onBlogEntry($entry) {
    $entryData = json_decode($entry, true);
echo "gallogallo";
    // If the lookup topic object isn't set there is no one to publish to
    if (!array_key_exists($entryData['category'], $this->subscribedTopics)) 
{
        return;
    }

    $topic = $this->subscribedTopics[$entryData['category']];

    // re-send the data to all the clients subscribed to that category
    $topic->broadcast($entryData);
}

Я не получаю никаких ошибок или предупреждений.Когда я пытаюсь вызвать addblog.php, ничего не происходит, и я не понимаю, почему.

Может быть, есть какие-нибудь намеки?Я тестирую это с XAMPP и Windows 10.

1 Ответ

0 голосов
/ 16 ноября 2018

Не знаю почему, но на другой системе это работает очень хорошо ...

...