如何减少聊天的AJAX请求数量?
我的网站上有一个聊天室,它需要向我的服务器发送许多 AJAX 请求(每秒至少 2 次),以检查是否有新消息。
有没有办法减少请求数量并仅在发布消息后重新加载消息?
我知道不可能使用套接字(因为我不能使用 flash、java 或 HTML5 的功能),但也许有一些处理 的技巧HTTP 的保持活动选项1.1?
I have a chat on my web site and it needs to send many AJAX requests to my server (at least 2 times per second) in order to check if there are new messages.
Is there a way to reduce the number of requests and reload messages only after they have been posted?
I know that there are no possibility to use sockets (because I can't use flash, java or features of HTML5), but maybe there is some trick dealing with keep alive option of HTTP 1.1?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,有一个简单的方法可以做到这一点。实际上,您所做的是将 ajax 调用的超时时间增加到较长的超时时间(例如 5 分钟)。您的服务器接收请求,然后保留它,偶尔检查新的响应。然后,当需要新的响应时,它只需响应请求,您的客户端就会收到更新。
如果 5 分钟内没有响应,您的客户端就会超时并启动新的 ajax 请求。如果您的服务器在 5 分钟内没有响应,通常会终止保留的请求以将其从队列中取出。
有效的“服务器端”推送。
Yes, there is an easy way to do this. Effectively, what you do is you increase the timeout for your ajax call to a long timeout (say, 5 minutes). Your server receives the request and then holds it, occasionally checking for new responses. Then, when a new response is warranted, it simply responds to the request, and your client receives the update.
If no response is had within 5 minutes, your client simply timeouts and starts a new ajax request. Your server, if it hasn't responded within 5 minutes, usually then just terminates the held request to get it out of the queue.
Effectively 'server-side' pushing.