如何在充当监听器的脚本中查看变量的内容:Facebook 实时更新

发布于 2024-12-08 19:58:56 字数 694 浏览 1 评论 0原文

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

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

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

I have two files, index.php and a callback.php. Index.php is used to display data on the website. The callback.php is a script that acts like a listener. The callback.php is called by a server and the $update variable in callback.php is updated by the server in real time. I want to see the contents of $update variable every time it updates.

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

How can i do this? Please let me know. I would appreciate if you have some sample code i could start with.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埋情葬爱 2024-12-15 19:58:56

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

基本上,客户端向服务器发送 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 来触发这些事件。

The technique I believe you are describing is called long-polling.

Basically, the client sends an ajax request to the server and sits there waiting for a response. Upon some event (this is where SSJS like Node comes in really handy), the server sends the client the new data. Upon receiving the data, the client immediately sends a new request and waits for the next update.

Here is the flow:

Page loads:

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

Then what? Suspense:

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

Someone else updates content you want to see in real time:

                     __________
                    |          |
                    | 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 ---> |         |
|___________|                            |_________|

Rinse, Repeat
The thing to remember here is that HTTP1.1 supports a persistent connection, meaning it won't timeout unless you want it to. I'd recommend looking into NodeJS on your server side to trigger these events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文