php - Ratchet 推送服务器教程问题

标签 php zeromq ratchet

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

http://socketo.me/docs/push

我正在按照教程所说的做,但我的订阅者不会收到任何消息。

我的服务器.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 对此进行测试。

最佳答案

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

关于php - Ratchet 推送服务器教程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53339484/

相关文章:

php - 如何避免图片显示在一栏中

python - zeromq (zmq) 缺少带有 c++ 发布者和 python 订阅者的消息

c++ - 以 "almost always auto"样式初始化 ZeroMQ 2.2 消息要使用私有(private)构造函数

c++ - ZeroMQ 无法通过单播 IPv6 工作

php - WebSocket 连接失败:连接建立时出错:net::ERR_CONNECTION_TIMED_OUT

php - CodeIgniter 上 Assets 文件夹的路径

php - 避免同一个 JS 文件的多个实例

php - html 类的 $outputstring 错误

facebook - 如何像facebook一样发出实时通知?

php - 如何让 Ratchet WebSocket 保持事件状态?