存储“在线用户”信息聊天应用程序列表(php/ajax)
我有几个聊天室。目前,我将聊天用户列表存储在 php 变量中。如果用户进入或离开房间,则该用户的姓名将从该列表中添加/删除。 为了使此存储持久化,我使用 memcached。为了更新聊天室的状态并将用户列表发送给聊天室中的所有用户,我使用定期的 ajax 请求将用户列表提取到聊天中的用户的浏览器中。
效果还不错。但我怀疑,如果聊天中有几百人,那么每 XX 秒向每个人发送一次完整的聊天用户列表是否是一个好主意。
聊天室通常如何处理这个问题?
I have several chat rooms. Currently, I store the list of chat users in a php variable. If a user enters or leaves the room, the user's name is added/removed from that list.
To make this storage persistent, I use memcached. To update the status of the chat room and send the user list to all users in the chat room, I use periodical ajax requests that fetch the user list into the browsers of the users who are in the chat.
It works okay. But I doubt that sending the whole list of chat users to everybody every XX seconds is a good idea if there are a couple of hundred people in the chat.
How are chat rooms usually dealing with this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不知道他们是怎么做的,但这里有一些方法可以。请记住在优化之前和之后进行测量。
Have no idea how they do, but here are some ways you could. Remember to measure before and after you optimize.
我运行聊天室的方式是为所有新消息以及新的登录和注销添加时间戳。
当用户进入房间时,我会下载完整列表。
我会定期(通过带有 JSON 调用的 AJAX)下载任何新事件(消息、登录和注销)。并相应更新相关列表。
The way I run my chat rooms, I timestamp all the new messages and new sign ins and sign outs.
When a user enters a room, I download the full list.
Periodically(via an AJAX with JSON call), I will download any new events (messages, logins and logouts). And update the relevant lists accordingly.
老实说,你所做的听起来不错。继续做你正在做的事情。也许跟踪最后一个列表,并且仅在最近 x 时间发生更改时才发送更新?如果你真的想节省几个字节。
Honestly, what you are doing sounds pretty good. Keep doin what ya doin. Maybe keep track of the last list and only send an update if they have changed in the last x time? If you really want to save a few bytes.
我也会通过聊天消息所经过的同一渠道推送列表更新。如果它是一个无限加载的页面,也许你可以在页面中注入类似这样的内容:
I would push the list updates through the same channel that the chat messages go through, too. If it's an endless-loading page, maybe you could inject something like this in the page:
感谢您的所有建议。此外,我将研究运行彗星服务器。
长轮询方法无法很好地扩展。
Thanks for all the suggestions. Additionally, I will look into running a comet server.
The long-polling approach just would not scale well.