有没有一种方法可以使用 HTTP 允许服务器更新客户端浏览器中的内容,而无需客户端请求?
通过发送 jQuery ajax 请求并更新新内容来更新界面非常容易。但我需要更具体的东西。
我想在客户端没有请求的情况下将响应发送给他们,并在他们在服务器上发现新内容时更新内容。无需每次都发送ajax请求。当服务器有新数据时,它会向每个客户端发送响应。
有没有办法使用 HTTP 或浏览器内的某些特定功能来做到这一点?
It is quite easy to update the interface by sending jQuery ajax request and updating with new content. But I need something more specific.
I want to send the response to client without their having requested it and update the content when they have found something new on the server. No need to send an ajax request every time. When the server has new data it sends a response to every client.
Is there any way to do this using HTTP or some specific functionality inside the browser?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Websockets、Comet、HTTP 长轮询。
Websockets, Comet, HTTP long polling.
它有名称服务器推送(您也可以在彗星技术名称下找到它)。使用这些关键字进行搜索,您会发现大量示例、工具等。为此不需要特殊协议。
It has name server push (you can also find it under name Comet technology). Do search using these keywords and you will find bunch examples, tools and so on. No special protocol is required for that.
RFC 6202 可能是一本不错的读物。
RFC 6202 might be a good read.
啊啊!您正试图打破 Web 的原则:) 您会看到,如果 Web 是纯 MVC(模型-视图-控制器),“服务器”实际上可以向客户端发送消息并要求它们更新。问题是服务器可以进行负载平衡,并且相同的请求可以发送到不同的服务器。现在,如果您要将消息发送回客户端,您必须知道谁都连接到服务器。假设该网站非常受欢迎,每天约有 100,000 人连接到该网站。实际上,您必须存储每个人的 IP,才能了解它们在互联网上的位置,并能够向它们“推送”消息。
注意事项:
。您会看到,它保持了简单和轻松。您可能会觉得客户端“轮询”服务器来拉取数据效率低下,并且您更喜欢推送,但服务器的设计得到了简化:)
我建议 ajax 拉取是最好的方法 - 您正在分发计算给客户端并保持简单(KIS 原则:)
当然你可以绕过它,问题是,值得吗?
希望这有帮助:)
Aaah! You are trying to break the principles of the web :) You see if the web was pure MVC (model-view-controller) the 'server' could actually send messages to the client(s) and ask them to update. The issue is that the server could be load balanced and the same request could be sent to different servers. Now if you were to send a message back to the client you'll have to know who all are connected to the server. Let's say the site is quite popular and you have about 100,000 people connecting to it every day. You'll actually have to store the IPs of each of them to know where on the internet they are located and to be able to "push" them a message.
Caveats:
You see, it keeps it simple and restful. You may feel it's inefficient for the client to "poll" the server to pull the data and you'd prefer push, but the design of the server gets simplified :)
I suggest ajax-pulling is the best way to go - you are distributing computation to the client and keeping it simple (KIS principle :)
Of course you can get around it, the question is, is it worth it?
Hope this helps :)