有没有一种方法可以使用 HTTP 允许服务器更新客户端浏览器中的内容,而无需客户端请求?

发布于 2024-11-18 01:22:00 字数 183 浏览 0 评论 0原文

通过发送 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不交电费瞎发啥光 2024-11-25 01:22:00

Websockets、Comet、HTTP 长轮询。

Websockets, Comet, HTTP long polling.

苍暮颜 2024-11-25 01:22:00

它有名称服务器推送(您也可以在彗星技术名称下找到它)。使用这些关键字进行搜索,您会发现大量示例、工具等。为此不需要特殊协议。

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.

灼痛 2024-11-25 01:22:00

RFC 6202 可能是一本不错的读物。

RFC 6202 might be a good read.

咿呀咿呀哟 2024-11-25 01:22:00

啊啊!您正试图打破 Web 的原则:) 您会看到,如果 Web 是纯 MVC(模型-视图-控制器),“服务器”实际上可以向客户端发送消息并要求它们更新。问题是服务器可以进行负载平衡,并且相同的请求可以发送到不同的服务器。现在,如果您要将消息发送回客户端,您必须知道谁都连接到服务器。假设该网站非常受欢迎,每天约有 100,000 人连接到该网站。实际上,您必须存储每个人的 IP,才能了解它们在互联网上的位置,并能够向它们“推送”消息。

注意事项:

  • 如果他们不再浏览您的网站怎么办?您会看到,如果您关闭浏览器,目前无法自动注销。服务器需要在固定超时后检查您是否已注销(或者您在每次响应时发送一个新的随机数以防止服务器执行该检查)
  • 系统重新启动/崩溃等情况如何?您将丢失您跟踪的所有 IP,并且您回到了原点 - 您有人连接到您,但在您收到新请求之前,您无法真正“发送”他们的数据,而他们可能期望的数据是这样的根据您的型号。
  • 让我们以 Facebook 的新闻提要或靠近右上角的“最新”链接为例 - 有时,当您浏览墙时,您会看到最新消息旁边的数字上升了,或者新的“提要”出现在顶部你的墙贴!客户端定期向服务器发送请求以了解更新内容,而不是相反

。您会看到,它保持了简单和轻松。您可能会觉得客户端“轮询”服务器来拉取数据效率低下,并且您更喜欢推送,但服务器的设计得到了简化:)

我建议 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:

  • What if they are no longer browsing your website? You see currently there is no way to log out automatically if you close your browser. The server needs to check after a fixed timeout if you have logged out (or you send a new nonce with every response to prevent the server from doing that check)
  • What about a system restart/crash etc? You'd lose all the IPs that you were keeping track of and you are back to square one - you have people connected to you but until you receive new requests you can't really "send" them data when they may be expecting it as per your model.
  • Let's take an example of facebook's news feeds or "Most recent" link close to the top right - sometimes while you are browsing your wall you see the number next to most recent has gone up or a new 'feed' has come to the top of your wall post! It's the client sending periodic requests to the server to find out what was updated rather than the other way round

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文