javascript - 带有 Ratchet 的 PHP WebSockets - 示例不起作用

标签 javascript php linux websocket ratchet

先介绍一下背景。

  1. 我的目标是使用 Ratchet WebSockets 创建双向客户端-服务器通信。

  2. 我已经按照描述安装了 ratchet 和配套软件 here .

  3. 我已成功创建 Hello World 应用程序,如所述 here .

  4. 现在我正在尝试使用 this 创建推送功能教程。我复制了代码,稍作修改(修改在下面的代码注释中注明),安装了 ZMQ 库(最新版本,将其添加到 php.ini,显示在 php -m 中 - 简而言之,它已正确安装)。但是 WebSocket 不起作用。

我将在下面提供我的测试过程以及指向我的域的真实实时链接,因此您可以自己检查。

  1. 我的推送服务器和他们教程里的一模一样,IP换成了我服务器的IP。我通过 SSH 运行它,它似乎连接正确。

  2. 我的 Pusher 类位于 MyApp 命名空间中,与他们的教程中的代码和相对位置相同。

  3. 我的 post.php 稍作修改,因为不需要为 MySQL 查询而烦恼:

$entryData = array(        //hard-coded content of $entryData for simplicity
    'cat'     => "macka"
  , 'title'   => "naslov"
  , 'article' => "tekst"
  , 'when'    => time()
);

// This is our new stuff
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
$socket->connect("tcp://light-speed-games.com:5555");       //my domain, still using port 5555 as in their example

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

此文件位于 here .

  1. 我的 client.php 与他们的相同,除了我必须为 IE 添加一个小修复程序以使用 when.js。我的问题与浏览器无关,与添加修复程序之前一样。
<script>
    window.define = function(factory) {    //my addition
        try{ delete window.define; } catch(e){ window.define = void 0; } // IE
        window.when = factory();
    };
    window.define.amd = {};
</script>
<script src="/apps/scripts/when.js"></script> 
<script src="http://autobahn.s3.amazonaws.com/js/autobahn.min.js"></script>
<script>
    var conn = new ab.Session(
        'ws://light-speed-games.com:8080' // The host (our Ratchet WebSocket server) to connect to
      , function() {            // Once the connection has been established
            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() {            // When the connection is closed
            console.warn('WebSocket connection closed');
        }
      , {                       // Additional parameters, we're ignoring the WAMP sub-protocol for older browsers
            'skipSubprotocolCheck': true
        }
    );
</script>

此文件位于 here .

理论上,应该发生是这样的(例如):我在​​打开控制台的情况下在 Chrome 中打开 client.php;然后我在 Firefox 中打开 post.php; Chrome 的控制台应显示消息“新文章已发布...”等(来自 client.php 中的 conn.subscribe 函数)。但是,当我这样做时,没有任何反应。连接保持打开状态(在我通过 SSH 关闭 push-server.php 之前不会显示“连接已关闭”错误)。控制台仍然是空的。

我想这就是过去几天的所有相关信息,其中很大一部分我都花在了弄清楚这个问题上。但是,我什至无法确定问题出在代码上还是出在我可能不知道的某些服务器配置设置上。所以,我来找你希望有人能给我指明正确的方向。

重要修改

我很确定问题出在 Autobahn.js 方法 conn.subscribe 无法正常工作。正在建立连接。当我将代码更改为:

function() {            // Once the connection has been established
            console.log('Connection established');
            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);
            });
        }

然后 Connection established 正确显示在控制台中。所以我认为我们需要对订阅方法进行故障排除。如果有人能向我解释它是如何工作的,以及“主题”和“数据”究竟应该是什么,那将有很大帮助。 Autobahn 文档使用 URL 作为此方法的参数(参见 here )。

最佳答案

您的客户正在寻找 kittensCategory 中的文章,但您发送的是 macka 类别。试试这个:

$entryData = array(
    'cat'     => "kittensCategory",
    'title'   => "naslov",
    'article' => "tekst",
    'when'    => time()
);

关于javascript - 带有 Ratchet 的 PHP WebSockets - 示例不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20764078/

相关文章:

php - 我如何在 ZF2 的 Zend\ServiceManager 中使用 Zend\Di?

php - 由于两个 select 语句,分页不起作用

linux - 在 shell 脚本中,如何在执行到标准输出(不是标准错误)时回显 shell 命令?

javascript - 如何从文本中删除空格、标点符号和符号

javascript - req.user 未定义

javascript - 是否可以使 mongoDb objectId 大于 24 个字符?

php - Zend 框架 url 重定向

javascript - 如何在 D3/javascripts 中将一列绘制为 x 轴,另一列绘制为 y 轴

linux - 删除早于 X 分钟的文件

linux - Windows 7共享ubuntu目录但看不到里面的文件怎么办?