如何获得“长轮询”与 Flex 3 和 php 一起使用吗?
我正在为一个客户开发一个项目,他们的 cms 将是 flex 3 和 php。将会有多个客户同时使用它,我正在努力做到当一个人做出改变时,其他人就会看到它。
我的问题是:如果我使用 Flex 3 和 PHP,我如何实现类似“服务器推送”的功能。长轮询是一个可能/好的选择吗?或者还有其他选项可以帮助我完成相同的任务吗?
任何帮助将不胜感激!
I am working on a project for a client whereby their cms will be flex 3 and php. There will be multiple clients using it at once and I am trying to make it to where when one person makes a change, the others will see it.
My Question Is: If I am using Flex 3 and PHP, how can I implement something that acts like 'server-push'. Is long polling a possible/good option? Or are there other options out there that can help me do the same task?
Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
长轮询(又名 AJAX Comet)和 PHP 的问题在于 PHP 缺乏真正使其成为可能的线程架构。许多其他传统语言都是为了以串行方式获取请求并快速响应而构建的。
较新的语言已经达到可以完全支持这个概念的程度。事实上,您确实需要一个使用 EPOLL(基于事件/异步套接字 IO)和/或微线程连接处理的框架和网络服务器,以便您可以一次处理数千个打开的连接,而不必为每个连接专用一个完整的操作系统线程。打开连接到您的服务器。 (操作系统线程的资源有限)
我制作了一个视频来解释 AJAX Comet/长轮询的概念。您可以在此处查看更多信息:http://www.youngtechstars.com/?p=466< /a>
现在...我确实为使用 PHP 进行长轮询的方法创建了真正的 hacky 概念证明。它需要使用自定义 NGINX mod。首先,请求将进入 NGINX 服务器,该服务器会将请求分派给 PHP 进程(使用 FastCGI)。如果我希望连接保持打开状态,我将返回带有自定义 HTTP 标头的响应(在本例中为“X-NGINX-WAIT-UNTIL-KEY:a10x39”或其他内容)。这将指示 NGINX 保持连接并且尚未向用户返回响应。在稍后的某个时刻,当数据准备好并传入密钥时,我会从 PHP(或另一个进程)打开一个返回 NGINX 的套接字。这样做会导致 NGINX 发出第二个请求,并在内部使用相同的请求再次访问 PHP 进程,以获取用户的响应。这是一个概念验证,并且工作完美,但我从未发布过它。此模型在集群 Web 环境中存在问题,因为您必须跟踪具有开放连接的服务器。
Flex 与在普通 JavaScript 中执行 XmlWebRequest 一样,长轮询不存在任何问题,因为请求是异步处理的。
然而,有许多框架可以开箱即用地支持此功能:
The problem with long polling (aka AJAX Comet) and PHP is that PHP lacks a threading architecture that really makes it possible. Many of the other traditional languages are built to get a request and respond quickly in a serial manner.
Newer languages are getting to a point where they can support the concept fully. In fact you really want a framework and webserver that uses EPOLL(Event based/Async Socket IO) and/or microthreaded handling of connections so that you can handle thousands and of open connections at once without having to dedicate a full operating system thread to each open connection hitting your server. (OS threads have a finite resources)
I did a video on explaining the concepts of AJAX Comet/Long polling. You can see more on it here: http://www.youngtechstars.com/?p=466
Now... I did create really hacky proof of concept for way to do long polling with PHP. It required the use of a custom NGINX mod. First the request would come into a NGINX server which would dispatch the request off to a PHP process (using FastCGI). If I wanted the connection to stay open I would return a response with a custom HTTP header (in this case "X-NGINX-WAIT-UNTIL-KEY: a10x39" or something). That would signal NGINX to hold on to the connection and not return a response to the user yet. At some point later I would open a socket back to NGINX from PHP (or another process) when data was ready and pass in the key. Doing this would cause NGINX to do a second request and hit the PHP process again with the same request internally to the get the response for the user. It was a proof of concept and worked flawlessly but I never released it. There are issues with this model in clustered web environments because you have to track the server that has the open connection.
Flex, as with doing XmlWebRequest in plain-old javascript, isn't any issue with long polling since the request is handled asynchronously.
There a number of frameworks that can support this out of the box however: