我的基于 Flash 的聊天客户端不够强大?还有什么办法呢?
我开发了一个基于 Flash 的聊天客户端,它显示属于某个组的用户发布的消息以及他们的用户名和撰写时间。数据与组 ID、用户 ID 和消息一起存储在 mysql 数据库中,以便在聊天会话之外持续存在,并允许用户稍后登录查看讨论,而不是聊天,更多的是公告板。当用户写消息时,我希望聊天客户端立即更新,看起来像是实时聊天。到目前为止,我的解决方案是在 Flash 代码中包含一个间隔,该间隔调用一个 PHP 页面,该页面在数据库中查询新评论并将其返回给 Flash。
我认为从我读到的内容来看,这种方法称为长轮询?对吗?这对于体积来说足够强大吗?当发生变化时,我是否会更好地考虑将数据推送给客户端?我如何检测这些变化?例如,我看过 APE,但我不认为它会将消息存储在数据库中。
有什么建议吗?
I have developed a Flash based chat client that displays messages posted by users belonging to a group along with their username and time of writing. The data is stored in a mysql database with the group id, user id and message so that it persists beyond chat sessions and allows users logging in at a later time to see the discussions, less of a chat, more of a notice board. When a user writes a message I want the chat clients to update instantly an appear to be real time chat. My solution so far has been to include an interval in my flash code that calls a PHP page which queries the database for new comments and return this to Flash.
I think from what Ive read that this approach is called long polling? is that right? Is this robust enough for volume? Would I be better looking at pushing the data to the client when there are changes? How do I detect these changes? I have looked at APE for example but I dont think that this stores the messages in a database.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
轮询的坏处是,它很快就会变得昂贵。
假设您有 10000 个用户在线,他们希望聊天延迟不超过 1 秒。然后youn将以每秒10000个请求轰炸你的服务器。
对于较小的事情或不需要更新的事情,轮询是一个很好的方法,因为它简单并且不会真正出错。
如果这只是针对最多 100 人使用聊天的小社区,那么这应该不是问题。
除此之外,您可以使用APE或SmartFox,或Red5或许多不同的东西来创建持久中继服务器,即与所有客户端具有永久连接并通知它们任何更改(例如新消息)的服务器。
一如既往,我个人的建议是使用 Haxe。您可以使用聊天教程开始。 Haxe 的学习曲线相当陡峭,但我认为这是完全值得的。我很高兴把 ActionScript 和 PHP 都抛在了后面。
编辑:您所描述的不是长轮询。另外,您很难使用 PHP 进行长轮询,至少如果传统地与 Apache 一起使用的话。 Apache 会创建一定数量的 PHP 进程。每当请求到达时,它都会寻找空闲的 PHP 进程并让它处理该请求。 PHP 进程完成后,会将响应发送回客户端。如果没有可用的空闲进程,它将缓冲该请求,直到有可用进程为止。因此,如果您尝试以经典方式使用 PHP 进行长轮询,则可能会完全阻止整个服务器。
The bad thing about polling is, it can become expensive quite fast.
let's assume, you have 10000 users online, who want the chat to have a delay no bigger than 1 second. Then youn will bombard your server with 10000 requests per second.
for smaller things or things that needn't be up to date, polling is a good approach, since its simple and can't really go wrong.
if this is just for a small community with upto 100 people using the chat, then this should not be the problem.
apart from that, you could use APE or SmartFox, or Red5 or a lot of different things to create a persistent relay server, i.e. a server that has permanent connections to all clients and notifies them of any changes (e.g. new messages).
as always, my personal advice is to use Haxe. You can use the chat tutorial to get started. Haxe has quite a steep learning curve, but I think it's totally worth it. I'm happy to have left both ActionScript and PHP behind.
edit: what you describe is not long polling. Also, you can hardly do long polling with PHP, at least if classicaly used with Apache. Apache will create a certain number of PHP processes. Any time a request arrives, it'll look for a free PHP process and let it process the request. When the PHP process is done, it sends the response back to the client. If there are no free processes available, it will buffer the request until a process becomes available. Thus you could totally block your whole server if you tried doing long polling with PHP the classical way.
如果您想在收到新消息时通知应用程序,那么您必须在服务器上实现某种形式的应用程序来监视数据库/收到数据库更改的警报,然后将更新传递给客户端。
这应该比仅仅轮询更有效,因为如果你考虑低流量场景,无论如何,通过轮询你都会把服务器搞得一团糟——这样你只有在有理由的时候才会产生流量。 ..
当您输入评论时,您的客户端可以直接(通过网络服务)通知该应用程序更改,然后该帮助应用程序可以更新数据库...
应该说,虽然我不是聊天专家计划 - 从未亲自参与过任何一项计划...
If you wanted to notify the application whenever a new message was received then you would have to implement some form of application on the server to monitor the database / be alerted of changes in it, and then pass updates out to clients.
This should be more efficient than just polling, because if you you think of a low traffic scenario, with polling you'll be knocking the hell out of the server regardless - this way you're only generating traffic when there is a reason to...
When you type in a comment, your client could notify this application directly (via a web service) of the change, and this helper app could then update the database...
It should be said though I'm no expert on chat programs - never been involved in one personally...