在哪里保存聊天/喊话箱的消息?
我将创建公共聊天/喊话框,几秒钟后刷新(几乎像 IRC 或其他东西)。
我的计划:
1)HTML表单,
2)使用JavaScript不允许正常提交该表单,
3)保存提交的消息(这里出现问题),
4)显示新消息(我猜是使用AJAX);
我不知道在哪里保存这些消息!我可以将它们保存在数据库中,但是......这可能非常慢,因为每个用户每隔几秒钟请求新消息,对吗?我可以尝试优化,但我不确定如何...也许我可以将这些消息保存在其他地方?
I'm going to create public chat/shout-box that will refresh after several seconds (almost like IRC or something).
My plan:
1) HTML form,
2) With JavaScript disallow to normally submit that form,
3) Save submitted message (here come problems),
4) Show new messages (with AJAX, I guess);
I'm not sure where to save those messages! I could save them in database, but... that may be very slow, because each user request new messages each several seconds, right? I could try optimize, but I'm not sure how... Maybe I could save those messages somewhere else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不明白为什么数据库访问会很慢(假设它位于网络服务器本地) - 我的意思是,如果数据无论如何都被永久访问并且数据库经过充分优化以有效处理查询,那么数据肯定会存储在 RAM 中。
显然,您可以将数据存储在自己的数据结构中,然后定期将其保存到数据库中,但是您正在重新发明轮子,如果您的服务器崩溃,您可能会丢失数据。
我个人会做的是将数据推送给您的客户,而不是从他们那里提取数据(这似乎是您打算做的),这样您只需在有人登录或出现新消息时发送数据 - 两者都可以不应该经常发生的情况。
I don't see why the database access would be slow (assuming it's local to the webserver) - I mean the data would certainly be stored in RAM if the data is accessed permanently anyhow and databases are quite optimized to handle queries efficiently.
Obviously you could store the data in your own datastructure and then save that to the db regularly, but you're reinventing the wheel and if your server crashes you may lose data.
What I personally would do is push data to your clients and NOT pull data from them (which seems what you're planning to do), that way you'd only have to send data whenever someone logs in or a new message appears - both situations that should happen not that often.