In the HTML5 specification there are something called websockets that many browsers already have implemented. Here is a PHP project supporting websockets: phpwebsocket. There are some javascript libraries which can use a flash component if the browser do not support websockets (and therefore be backwards compatible)
imho webSockets is the way to go, although PHP might not be the most suitable backend to handle them.
If you know how to use Sockets then use them; otherwise using AJAX in a timer should suffice so long as it doesn't need to be real-time (5-10 second delays between fetching messages should be OK).
As pointed out by @jgauffin you could look doing this on your existing PHP server using a Comet using the Comet and PHP option or WebSocket Solution using something like phpwebsocket.
You could create another component dedicated to handling your real-time messaging such as a dedicated Comet or WebSockets process running on you existing hosting or a wholly dedicated server. If you take this approach you could look at using something like socket.io running in node.js or a full third party Comet or WebSockets server.
Finally, you could have a look at a hosted service which would allow you to keep your existing development and production environment and you could then layer on the real-time chat. One such service called Pusher (which I work on) handles all the real-time messaging infrastructure in addition to providing added functionality specifically developed for making chat applications easy to build. It also means that you don't need to upgrade your hosting if you are using something like shared hosting where things are tightly locked down and you can't easily install your own components.
发布评论
评论(4)
最常用的方法称为 COMET。
这是一个 PHP 实现: How to Implement COMET with PHP
在 HTML5 规范中有一些内容称为 websockets,许多浏览器已经实现了。下面是一个支持 websocket 的 PHP 项目:phpwebsocket。如果浏览器不支持 websockets(因此向后兼容),有一些 javascript 库可以使用 flash 组件,
恕我直言,websocket 是最佳选择,尽管 PHP 可能不是最适合处理它们的后端。
The most frequently used method is called COMET.
Here is a PHP implementation: How to implement COMET with PHP
In the HTML5 specification there are something called websockets that many browsers already have implemented. Here is a PHP project supporting websockets: phpwebsocket. There are some javascript libraries which can use a flash component if the browser do not support websockets (and therefore be backwards compatible)
imho webSockets is the way to go, although PHP might not be the most suitable backend to handle them.
如果您知道如何使用
Socket
,那么就使用它们;否则,只要不需要实时,在计时器中使用 AJAX 就足够了(获取消息之间有 5-10 秒的延迟应该可以)。If you know how to use
Sockets
then use them; otherwise using AJAX in a timer should suffice so long as it doesn't need to be real-time (5-10 second delays between fetching messages should be OK).我建议为此使用 Socket.IO。
I'd suggest using Socket.IO for this.
PHP 有几个选项:
You have a few options from PHP: