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

这是一些背景。

  1. 我的目标是使用棘轮WebSockets创build双向客户端 – 服务器通信。

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

  3. 我已经成功创build了一个Hello World应用程序,如下所述。

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

我会给我的testing过程提供真实的链接到我的域下面,所以你可以自己检查一下。

  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)); 

这个文件位于这里 。

  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> 

这个文件位于这里 。

从理论上讲, 应该发生这样的事情 (例如):我在​​Chrome中打开client.php并打开控制台, 然后我在Firefox中打开post.php ; Chrome的控制台应该显示消息“New article published …”(来自client.phpconn.subscribe函数)。 但是,当我这样做,没有任何反应。 连接保持打开状态(直到我通过SSHclosurespush-server.php后才显示“连接closures”错误)。 控制台保持空白。

我想这就是过去几天的所有相关信息,其中很大一部分我花在了试图解决这个问题上。 但是,我甚至无法确定问题是否出现在代码中,或者我可能不知道某些服务器configuration。 所以,我来找你,希望有人指点我的方向。

重要的编辑

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

 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作为此方法的参数(请参阅此处 )。

在端口8080上看到您的主机light-speed-games.com无法正常工作是否正确? 如果没有,我会建议解决这个问题,因为它可能会导致你的问题。

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

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