带有棘轮的PHP WebSockets - 示例不起作用

jov*_*van 3 javascript php linux websocket ratchet

这是一些背景知识.

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

  2. 我已经安装了棘轮和附带的软件,如所描述这里.

  3. 我已经成功地创建了一个Hello World应用程序的描述在这里.

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

我将在下面的测试过程中提供真实的实时链接,以便您自行查看.

  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));
Run Code Online (Sandbox Code Playgroud)

此文件位于此处.

  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>
Run Code Online (Sandbox Code Playgroud)

此文件位于此处.

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

我认为这是过去几天的所有相关信息,其中很大一部分用于试图解决这个问题.但是,我甚至无法确定问题是否与代码或某些服务器配置设置有关,我可能不知道.所以,我来找你,希望有人能指出我正确的方向.

重要的编辑

我很确定问题是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);
            });
        }
Run Code Online (Sandbox Code Playgroud)

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

mat*_*exx 7

您的客户正在寻找一篇文章kittensCategory,但您正在发送类别macka.试试这个:

$entryData = array(
    'cat'     => "kittensCategory",
    'title'   => "naslov",
    'article' => "tekst",
    'when'    => time()
);
Run Code Online (Sandbox Code Playgroud)