在 PHP 中使用 Comet? [英] Using comet with PHP?

查看:34
本文介绍了在 PHP 中使用 Comet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was thinking of implementing real time chat using a PHP backend, but I ran across this comment on a site discussing comet:

我的理解是 PHP 是一个彗星的可怕语言,因为Comet 要求你保持一个对每个人开放的持久连接浏览器客户端.使用 mod_php 这个意味着捆绑一个阿帕奇孩子每个客户的全职工作根本不缩放.我的人知道做彗星的东西大多是使用设计的 Twisted Python处理数百或数千同时连接.

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.

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

Is this true? Or is it something that can be configured around?

推荐答案

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

Agreeing/expanding what has already been said, I don't think FastCGI will solve the problem.

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

Each request into Apache will use one worker thread until the request completes, which may be a long time for COMET requests.

这篇关于 Ajaxian 的文章 提到在 Apache 上使用 COMET,以及这很难.该问题并非特定于 PHP,而是适用于您可能希望在 Apache 上使用的任何后端 CGI 模块.

This article on Ajaxian mentions using COMET on Apache, and that it is difficult. The problem isn't specific to PHP, and applies to any back-end CGI module you may want to use on Apache.

建议的解决方案是使用 'event' MPM 模块这改变了请求被分派到工作线程的方式.

The suggested solution was to use the 'event' MPM module which changes the way requests are dispatched to worker threads.

这个 MPM 试图修复HTTP 中的保持活动问题".在客户完成第一个请求,客户端可以保留连接打开,并进一步发送请求使用相同的套接字.这个可以节省大量开销创建 TCP 连接.然而,Apache 传统上保留一个完整的子进程/线程等待数据来自客户,它带来了自己的缺点.为了解决这个问题,这个 MPM 使用一个专用线程来处理监听套接字,和Keep Alive 中的所有套接字状态.

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.

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

Unfortunately, that doesn't work either, because it will only 'snooze' after a request is complete, waiting for a new request from the client.

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

Now, considering the other side of the problem, even if you resolve the issue with holding up one thread per comet request, you will still need one PHP thread per request - this is why FastCGI won't help.

您需要诸如 Continuations 之类的东西,它允许彗星请求在事件发生时恢复触发由观察到.AFAIK,这在 PHP 中是不可能的.我只在 Java 中见过它 - 请参阅 Apache Tomcat 服务器.

You need something like Continuations which allow the comet requests to be resumed when the event they are triggered by is observed. AFAIK, this isn't something that's possible in PHP. I've only seen it in Java - see the Apache Tomcat server.

有一篇文章是关于使用负载平衡器 (HAProxy) 允许您同时运行 apache 服务器和支持 Comet 的服务器(例如jetty, tomcat for Java) 在同一台服务器的 80 端口上.

There's an article here about using a load balancer (HAProxy) to allow you to run both an apache server and a comet-enabled server (e.g. jetty, tomcat for Java) on port 80 of the same server.

这篇关于在 PHP 中使用 Comet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆