PHP:监听 HTTP 请求/HTTP 流

发布于 2024-12-29 18:54:06 字数 314 浏览 1 评论 0原文

最近我阅读了一些有关 Ajax HTTP 流的内容。如果我理解得很好的话,HTTP 流是通过向服务器发送 Ajax 请求来实现的。服务器启动一个 PHP 脚本,该脚本不会终止,但会定期刷新其内容,以便浏览器可以接收任何输出。

然而,当许多客户端使用 HTTP 流连接到服务器时,它将消耗相当多的内存,因为每个连接都需要它自己的 PHP 进程。所以我在想,是否有可能有一个 PHP 脚本来监听任何传入的 HTTP 请求?在这种情况下,该脚本可以处理多个连接,因此可以节省内存。如果这是可能的,这种方法有什么缺点?还有其他方法可用吗?

注意:目前我没有现实世界的例子,我只是在尝试这个。

Recently I've been reading a little about Ajax HTTP streaming. If I understood everything well HTTP streaming is achieved by sending an Ajax request to the server. The server starts a PHP script, and that script just won't terminate, but will flush it's content regularly so the browser can receive any output.

However, when many clients connect to a server using HTTP streaming it will consume quite some memory, since each connection needs it's own PHP process. So I was thinking, is it perhaps possible to have one PHP script which listens to any incoming HTTP requests? In that case that script can handle multiple connections and it will therefore save memory. And if this is possible, what are the downsides to this method? And are there any other methods available?

Note: currently I don't have a real world example for this, I'm just experimenting with this.

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

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

发布评论

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

评论(1

从此见与不见 2025-01-05 18:54:06

您提到的问题称为推送通知与拉取通知。拉 - 是当客户端向服务器发送请求时 - 并且服务器响应,而推 - 是当服务器发起通信时。

在我看来,您正在寻找“推送”解决方案。使用以下链接查看如何设置 UDP 连接:
http://php.net/manual/en/function.socket-create.php

The problem that you refer to is called push vs. pull notification. Pull - is when the client is sending a request to the server - and the server responses, and push - is when the server initiate the communication.

It sounds to me that you're looking for a "push" solution. Use the following link to see how you can setup UDP connection:
http://php.net/manual/en/function.socket-create.php

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