处理 JS 聊天框的有效方法是什么?
假设我想编写一个 AJAX 聊天框,只是一些简单的东西,我写一些文本,然后将其发布。我们甚至不用太担心框中的用户名。
第一直觉告诉我只需编写一个表并将数据发布到其中,然后每隔几秒查询一次。
让我们假设这不是 2011 年,计算机很便宜,我们可以接受每 5 秒更新一次的缓慢滞后的聊天框,或者通过 SQL 风暴减慢整个服务器的速度。
有哪些替代方案?我可以用 XMPP 做点什么吗?如果除了我最喜欢的服务器端语言(和一些 SQL)之外我没有其他语言怎么办?我可以以某种巧妙的方式使用消息哈希吗?我可以做什么来限制请求数量?
我正在寻找一个很好的计划,用于编写基于网络的聊天框背后的软件,该计划非常资源高效且灵活。我确信我们正在为我们已有的聊天室做一些巧妙的事情。
Lets say I want to write an AJAX chat box, just something simple, I write some text, and it gets posted up. Lets not even worry too much about names of users in the box.
First instincts tell me to just write a table and post data into it, and query every few seconds.
Lets pretend this isn't 2011 and computers a cheap and we're okay with slow laggy chat boxes that update every 5 seconds, or slow the entire server down with a storm of SQL.
What are some alternatives? Could I do something with XMPP? What if I didn't have anything other than my favorite server-side language (and some SQL)? Could I use message hashes in some neat way? What can I do to limit the number of requests?
What I'm looking for is a great plan for writing the software behind a web-based chat box, that is very resource efficient, and flexible. I'm sure we're doing something neat here on SO for the chatrooms we already have.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题是你时不时地进行投票。您真正想要的是持续连接以传输数据。
您可以尝试使用 Comet 方法 - 一个可以保持打开状态以发送的流连接聊天消息下降。
还有一个称为长轮询的技巧 - 您保持连接打开,直到收到新消息,然后关闭它并开始新消息。
Your problem is you're polling every now and then. What you really want is a constant connection to stream data with.
You could try using the Comet method - a streaming connection you can keep open to send chat messages down.
There's also a trick called long-polling - You keep the connection open until you receive a new message, then close it and start a new one.