如何使用 XAJAX 和 PHP 编写实时聊天?
如何使用 XAJAX 和 PHP 编写实时聊天?
换句话说,有没有办法将xajax响应从服务器发送到多个客户端? 或者是客户端每隔几秒检查一次新消息的唯一可能性?
How can I write a real time chat using XAJAX and PHP?
In other words, is there a way to send xajax responses from the server to multiple clients?
Or is the only possibility to check for new messages every few seconds on client side?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不可以。客户端必须重复“轮询”服务器。
我认为这里的关键是思考交互设计。 诀窍是欺骗用户,让他们认为聊天是即时的,但实际上它每 1 秒、2 秒、3 秒或 10 秒更新一次。
思路:
1)当用户发送消息时,直接在聊天中展示并触发投票。
2) 如果民意调查返回来自其他用户的多条消息,请不要一次添加所有消息,实际上要在 1-2 秒左右的时间内添加它们,并且间隔随机,让它们看起来像是正在进来“即时”且独立。 (如果立即收到一堆消息,用户很快就会意识到聊天内容是立即更新的,而不是连续更新的。)
3) 如果用户空闲了 x 时间。 将轮询率降低到每 10 秒左右一次。
4) 如果用户活跃,即发送大量消息,则更频繁地轮询。
5) 为您写入聊天上次更新时间的每个频道建立一个静态文件。 例如,文件
chat-teenfun-lastupdate.txt
的内容为1224934239
或您喜欢的任何时间格式。 静态提供此文件并让客户端轮询此文件以检查通道是否已更新,而不是调用进行动态检查的chat-poll.php?ch=teenfun
。 静态文件的服务速度大约快 10-100 倍,具体取决于动态脚本所涉及的工作,当您有超过 250 个用户进行轮询时,您将需要它。祝好运并玩得开心点!
/0
PS。 或者,您实际上可以让客户端对服务器进行 ajax 调用,并使它们保持“挂起”状态。也就是说,您接受他们的请求并假装开始发回数据,但随后您只是暂停。 当发生某些事情时,您可以使用适当的数据完成响应。 为此,我相信您需要编写自己的 HTTP 服务器,具体是这样做的,因为您不能让 250 个 php 进程挂在内存中。 也许 Lighttpd 可以通过 LUA 缓存模块以这种方式使用。 我不知道。 不过会很有趣。 天哪,我得找个时间试试:)
No. Clients must "poll" the server repeatadly.
I think the key here is to think interaction design. The trick is to fool the user into thinking that the chat is instant, but when in reality it updates once every 1 or 2 or 3 or 10 seconds.
Ideas:
1) When the user sends a message, show it directly in the chat and trigger a poll.
2) If a poll comes back with multiple messages from other users, don't add them all at once, actually add them over a period of 1-2 sec or so, with random spacing, making it look like they're coming in "instantly" and independently. (If a bunch of messages come in at once the user is very quick to realize that the chat updated there and then, and not continiously.)
3) If the user is idle for x amount of time. Drop the poll rate to once every 10sec or so.
4) If the user is active, ie sending a lot of messages, poll more often.
5) Have a static file for every channel that your write the time the chat last updated to. For instance file
chat-teenfun-lastupdate.txt
has the contents1224934239
or whatever time format you prefer. Serve this file statically and let clients poll this file in order to check if the channel has updated, rather that callingchat-poll.php?ch=teenfun
that does a dynamic check. Static files are served around 10-100 times faster, depending on the work involved for the dynamic script, and you're gonna need it when you get 250+ users polling.Good luck and have fun!
/0
PS. Alternatively you could actually let clients do an ajax call to the server and keep them 'hanging'. That is you accept their request and pretend to start sending data back, but then you just pause. When something happends you finish the response with the approriate data. For this to work I believe you'd need to write your own HTTP-server though, that does this specifically, as you can't have 250 php processes hanging around in memory. Maybe Lighttpd could be used in this way somehow with that LUA cache mod. I don't know. Would be interesting though. Hell I gotta try it sometime :)
当然有,但我认为对于很多用户来说它不会非常有效。 您可以进行轮询,每个客户端轮询服务器以查看是否有任何新消息,或者您可以使用 comet 技术,服务器可以将新消息推送到客户端 - 查看 XAJAX 的 Comet 插件。 如何使用 XAJAX 和 PHP 实现这一点超出了我的范围,但以下是我尝试实现它的方法。
让每个客户端连接到服务器(登录等),然后:
使用像 ejabberd 这样的真正的 IM 服务器可以大有帮助,效率更高,并允许您的用户通过桌面客户端进行连接(如果这是您想要的)。 我可能会使用它作为后端,IOW ejabberd 将是服务器,PHP 将是使用 XMPP 的客户端在 PHP 中,并充当 webgui 的代理。
另请参阅:
Google Techtalk 关于 Gmail 聊天功能(以及可扩展性)问题)
这是我的 0.02 美元
Sure there is, but I don't think it'll be very efficient with many users. You can either do polling where each client polls the server to see if there are any new messages, or you could use the comet technique in which the server can push new messages to the clients - Check out the Comet plugin for XAJAX. How this would be implemented using XAJAX and PHP is beyond me, but here's how I would try to implement it.
Let each client connect to the server (login etc), then:
Using a true IM server like ejabberd could go a long way, be more efficient and allow your users to connect via desktop clients (if that's what you want). I'd probably use that as a backend, IOW ejabberd would be the server and PHP would be the client using XMPP in PHP, and act as a proxy for the webgui.
See also:
Google Techtalk on Gmail's chat feature (and scalability issues)
That's my $0.02
只要没有 HTTP 推送技术,您就永远无法仅使用
JavaScript
进行实时聊天。可能的解决方法:
Flash
Movie 或Java
Applet 执行一些套接字通信As long as there there is not HTTP push technology you will never get a realtime chat only using
JavaScript
.Possible workarrounds:
Flash
Movie or aJava
Applet to perform some socket communication您可以使用 websockets,但作为一项新的 HTML5 功能,它有点有限。 幸运的是,有 socksjs,它在不处理 websocket 的浏览器上实现了 websockets。
在托管方面,您应该能够使用任何 websockets 服务器,其中有一些用于 PHP。
You could use websockets, but being a new HTML5 feature it's kinda limited. Lucky for you there is socksjs, which implements websockets on browsers that do not handle it.
On the hosting side you should be able to use any websockets server, there's a few for PHP.
如果您希望实现一个用 PHP/JSP 等脚本语言编写的聊天服务器,则必须从您的选项列表中勾选挂起 HTTP 连接的技术。 原因是大多数网络服务器(特别是共享主机)不喜欢挂起太多连接。
您可以在此“服务器的优化聊天服务器协议”中找到实现 Web 客户端和 PHP 聊天服务器所需的一切Side Scripting Languages” 出版物。
If you are looking to implement a chat server written with a scripting language such as PHP/JSP, technique of hanging HTTP connection will have to be ticked off from your your options list. The reason is most of the web severs (specially shared hosts) don't like too many connections hanging.
You can find everything you need to implement a web client and PHP chat server in this "Optimized Chat Server Protocol for Server Side Scripting Languages" publication.
我见过的最佳策略是对消息执行 AJAX 请求,然后在完成后立即重新启动完全相同的请求。
在服务器端,使脚本“停止”60 秒或直到收到新消息。 这会使同一连接最多保持打开状态 60 秒,但当收到新消息时,它会输出该消息并立即停止,提示客户端 AJAX 打开另一个连接。
这提供了几乎即时的新消息通知,并且在服务器上也比每 x 秒建立一个新连接要容易得多。
The best strategy I've seen is to do an AJAX request for messages and then restart that exact same request as soon as it finishes.
On the server side, make the script "stall" for 60 seconds or until a new message is received. This keeps the same connection open for a max of 60 seconds, but when a new message is received, it outputs it and stops immediately, prompting the client-side AJAX to open another connection.
This provides almost instant notification of new messages and is also much easier on the server than making a new connection every x seconds.