php - 在 PHP 中使用 cometd ?

标签 php comet

我正在考虑使用 PHP 后端实现实时聊天,但我在一个讨论 comet 的网站上看到了这条评论:

My understanding is that PHP is a terrible language for Comet, because Comet requires you to keep a persistent connection open to each browser client. Using mod_php this means tying up an Apache child full-time for each client which doesn’t scale at all. The people I know doing Comet stuff are mostly using Twisted Python which is designed to handle hundreds or thousands of simultaneous connections.

这是真的吗?或者它是可以配置的东西吗?

最佳答案

同意/扩展已经说过的话,我不认为 FastCGI 会解决问题。

Apache

Apache 中的每个请求都将使用一个工作线程,直到请求完成,这对于 COMET 请求来说可能需要很长时间。

This article on Ajaxian提到在 Apache 上使用 COMET,这很困难。该问题并非特定于 PHP,并且适用于您可能希望在 Apache 上使用的任何后端 CGI 模块。

建议的解决方案是使用 'event' MPM module这改变了将请求分派(dispatch)给工作线程的方式。

This MPM tries to fix the 'keep alive problem' in HTTP. After a client completes the first request, the client can keep the connection open, and send further requests using the same socket. This can save signifigant overhead in creating TCP connections. However, Apache traditionally keeps an entire child process/thread waiting for data from the client, which brings its own disadvantages. To solve this problem, this MPM uses a dedicated thread to handle both the Listening sockets, and all sockets that are in a Keep Alive state.

不幸的是,这也不起作用,因为它只会在请求完成后“暂停”,等待来自客户端的新请求。

PHP

现在,考虑到问题的另一面,即使您解决了每个 comet 请求占用一个线程的问题,您仍然需要每个请求一个 PHP 线程 - 这就是 FastCGI 无济于事的原因。

你需要类似 Continuations 的东西这允许 cometd 请求在观察到它们触发的事件时恢复。 AFAIK,这在 PHP 中是不可能的。我只在 Java 中看到过它 - 请参阅 Apache Tomcat server .

编辑:

有一个 article here关于使用负载平衡器 ( HAProxy ) 允许您在同一服务器的端口 80 上同时运行 apache 服务器和支持 comet 的服务器(例如 jetty、用于 Java 的 tomcat)。

关于php - 在 PHP 中使用 cometd ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26903134/

相关文章:

javascript - HTML 图表与 MySQL 使用 Javascript PHP

ajax - 定期Ajax POST调用与COMET/Websocket推送

tomcat - Cloudfoundry,默认协议(protocol)

javascript - Gmail如何在Opera上做 cometd ?

php - OAuth 2.0 服务器 PHP,找不到接口(interface)

php - 将参数传递给 jQuery 函数?

php - Zend Framework 中的单元测试 json 输出

javascript - 如何使用 $http.post 将 json 数据发布到 php 脚本

javascript - 在页面正文中保持 jQuery .getJSON() 连接打开并等待?

PHP实时聊天与ajax轮询