简体   繁体   English

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

[英]PHP WebSockets with Ratchet - example doesn't work

Here's some background first. 这是一些背景知识。

  1. My aim is to use Ratchet WebSockets to create two-way client-server communication. 我的目标是使用Ratchet WebSockets创建双向客户端 - 服务器通信。

  2. I have installed ratchet and accompanying software, as described here . 我已经安装了棘轮和附带的软件,如所描述这里

  3. I have successfully created a Hello World application as described here . 我已经成功地创建了一个Hello World应用程序的描述在这里

  4. Now I am trying to create Push functionality using this tutorial. 现在我正在尝试使用教程创建Push功能。 I have copied the code, modifying it slightly (modifications noted in code comments below), installed the ZMQ library (latest version, added it to php.ini, show up in php -m - in short, it's installed correctly). 我已复制代码,稍微修改它(下面的代码注释中记录的修改),安装了ZMQ库(最新版本,将其添加到php.ini,显示在php -m - 简而言之,它已正确安装)。 But the WebSockets don't work. 但WebSockets不起作用。

I will give my testing process with real live links to my domain below, so you can check it yourself. 我将在下面的测试过程中提供真实的实时链接,以便您自行查看。

  1. My push server is exactly the same as the one in their tutorial, with the IP changed to my server's IP. 我的推送服务器与他们教程中的推送服务器完全相同,IP已更改为我服务器的IP。 I run this through SSH and it seems to connect correctly. 我通过SSH运行它,它似乎正确连接。

  2. My Pusher class is in the MyApp namespace, same code and same relative location as in their tutorial. 我的Pusher类位于MyApp命名空间中,与教程中的相同代码和相对位置相同。

  3. My post.php is slightly modified because there's no need to bother with MySQL queries: 我的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));

This file is located here . 此文件位于此处

  1. My client.php is the same as theirs, except I had to add a little fix for IE to work with when.js . 我的client.php和他们的一样,除了我必须为IE添加一些修复工具来使用when.js My problem is browser-independent and the same as it was before the fix was added. 我的问题与浏览器无关,与添加修复程序之前的问题相同。
<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>

This file is located here . 此文件位于此处

In theory, what should happen is this (for example): I open client.php in Chrome with console switched on; 从理论上讲, 应该发生的事情 (例如):我在​​Chrome中打开client.php并打开控制台; then I open post.php in Firefox; 然后我在Firefox中打开post.php ; Chrome's console should show the message 'New article published...' etc (from the conn.subscribe function in client.php ). Chrome的控制台应显示消息“发布新文章...”等(来自client.phpconn.subscribe函数)。 However, when I do this, nothing happens. 但是,当我这样做时,没有任何反应。 The connection remains open (doesn't show the 'connection closed' error until I switch off push-server.php through SSH). 连接保持打开状态(直到我通过SSH关闭push-server.php才显示“连接已关闭”错误)。 The console remains empty. 控制台仍然是空的。

I think that's all the relevant info from the last couple of days, a large portion of which I've spent on trying to figure this out. 我认为这是过去几天的所有相关信息,其中很大一部分用于试图解决这个问题。 However, I've been unable to even make sure if the problem is with the code or with some server configuration setting I may be unaware of. 但是,我甚至无法确定问题是否与代码或某些服务器配置设置有关,我可能不知道。 So, I come to you hoping someone will point me in the right direction. 所以,我来找你,希望有人能指出我正确的方向。

Important edit 重要的编辑

I'm pretty sure the problem is with the Autobahn.js method conn.subscribe not working properly. 我很确定问题出在Autobahn.js方法conn.subscribe无法正常工作。 The connection is being established. 正在建立联系。 When I change the code to: 当我将代码更改为:

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

Then Connection established is shown in the console correctly. 然后Connection established正确显示在控制台中。 So I believe we need to troubleshoot the subscribe method. 所以我认为我们需要对subscribe方法进行故障排除。 If someone can explain to me how it works, and what exactly "topic" and "data" are supposed to be, it would be of great help. 如果有人可以向我解释它是如何工作的,以及究竟应该是什么“主题”和“数据”,那将会有很大的帮助。 The Autobahn documentation uses a URL as an argument for this method (see here ). Autobahn文档使用URL作为此方法的参数(请参见此处 )。

Your client is looking for an article in kittensCategory , but you are sending category macka . 您的客户正在寻找kittensCategory的文章,但您正在发送类别macka Try this: 尝试这个:

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

Is it correct to see your host light-speed-games.com on port 8080 is not functioning? 在端口8080上看到主机light-speed-games.com无法正常工作是否正确? If not, I would suggest to fix this as it is likely its causing your issues. 如果没有,我建议修复此问题,因为它可能会导致您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM