棘轮推送服务器教程问题

时间:2018-11-16 14:09:39

标签: php zeromq ratchet

我正在尝试使棘轮推送教程起作用。

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 :(得分:-1)

我不知道为什么,但是它在另一个系统上运行得很好...