【问题标题】:How to see the content of a variable in a script that acts as a listener: Facebook real time update如何在充当侦听器的脚本中查看变量的内容:Facebook 实时更新
【发布时间】:2011-10-09 21:55:24
【问题描述】:

我有两个文件,index.php 和一个 callback.php。 Index.php 用于在网站上显示数据。 callback.php 是一个充当监听器的脚本。 callback.php 由服务器调用,callback.php 中的$update 变量由服务器实时更新。我想在每次更新时查看 $update 变量的内容。

回调.php:

    if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe' &&
        $_GET['hub_verify_token'] == VERIFY_TOKEN) {
      echo $_GET['hub_challenge'];

    } else if ($method == 'POST') {
       $updates = json_decode(file_get_contents("php://input"), true);
       //I want to see the content of $updates
       // resend the push notification again.
        error_log('updates = ' . print_r($updates, true));
     }

我该怎么做?请告诉我。如果您有一些我可以开始的示例代码,我将不胜感激。

【问题讨论】:

    标签: php facebook real-time server-side


    【解决方案1】:

    我相信您所描述的技术称为长轮询。

    基本上,客户端向服务器发送一个 ajax 请求并坐在那里等待响应。在某些事件发生时(这就是像 Node 这样的 SSJS 真正派上用场的地方),服务器会向客户端发送新数据。客户端收到数据后,立即发送新请求并等待下一次更新。

    流程如下:

    页面加载:

     __________                              _________
    |          |                            |         |
    | Client A | ---> Request for Data ---> | Server  |
    |__________|                            |_________|
    

    然后呢?悬念:

     __________                              _________
    |          |                            |         |
    | Client A | .......................... | Server  |
    | waiting  |                            | waiting |
    |__________|                            |_________|
    

    其他人实时更新您想看的内容:

                         __________
                        |          |
                        | Client B |
                        |__________|
                             |
                             | Sends update to Server
                             |___________________.
                                                 |
                                                 V
     __________                              _________
    |          |                            |         |
    | client A | ............<------------- | Server  |
    | waiting  |                            | Reacts  |
    |__________|                            |_________|
         |
         |
         V    
     __________                              _________
    |          |                            |         |
    | Client A |   ( No open connection)    | Server  |
    | Updates  |                            | Idle... |
    |__________|                            |_________|
         |
         |
         V
     ___________                              _________
    |           |                            |         |
    | Client A  |                            |         |
    | Reacts to |                            | Server  |
    | Update    | ---> Request for Data ---> |         |
    |___________|                            |_________|
    

    冲洗,重复 这里要记住的是 HTTP1.1 支持持久连接,这意味着它不会超时,除非你想要它。我建议在您的服务器端查看 NodeJS 以触发这些事件。

    【讨论】:

    • 是的……这似乎有点像我在寻找的东西。我的 index.php 订阅了一些实时数据到服务器,回调 url 是 callback.php。服务器将更改通知发送到 callback.php。我想查看存储在 $updates 中的更改通知。
    • 当您说服务器端时,您是指服务器本身还是 callback.php 文件?我不控制向 callback.php 发送更改通知的服务器。
    • 你必须在服务器上有某种事件监听器。由于阻塞 I/O,PHP 通常不是侦听器的最佳解决方案。例如,如果您希望 PHP 脚本每秒检查数据库中是否有其他用户的新帖子,则必须使用 sleep(1000),它会暂停脚本并阻止其他脚本运行。这是 NodeJS 的主要优点之一。
    猜你喜欢
    • 2011-07-06
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 2012-09-18
    • 2010-09-11
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    相关资源
    最近更新 更多