Gmail 的定期邮件提取是如何工作的?

发布于 2024-10-17 14:18:24 字数 145 浏览 1 评论 0原文

当我使用 gmail 时,即使我没有刷新页面,我刚刚收到的一些新邮件也会出现在收件箱中。我相信这会在 Ajax 中完成。

有没有与它非常相似的好的演示?定期检查和获取 JSON 数据(我不确定它是否是 JSON 数据..)以获取新数据?

谢谢!

When I am using gmail, some new mails that I just received appear on the inbox, even if I did not refresh the page. I believe that would done in Ajax.

Is there any good demo that works very similar to it? Periodically checking and getting JSON data (I am not sure if it is JSON data..) to fetch new data??

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

如梦初醒的夏天 2024-10-24 14:18:24

定期刷新的问题在于,虽然它对于某些时间要求不太严格的事情(例如电子邮件获取)很有用,但它不是即时的。因此,您不会想将它用于聊天之类的事情,因为等待响应甚至五秒钟都太长了。您可以减少轮询间隔并每秒甚至半秒发出一次请求,但这样您的浏览器很快就会过载并浪费资源。

一个解决方案是使用一种称为 ajax 长轮询的技术(其他人称为“comet”或“反向 ajax”)。使用这种技术,浏览器会发出持续时间较长的 ajax 请求,直到有新数据可用时才会返回。该请求位于服务器上(您需要运行特殊的服务器端软件来以可扩展的方式处理此问题,但您可以与 php 一起破解某些内容作为演示),直到新数据可用,此时它将返回给客户端新数据。当客户端收到数据时,它会向服务器发出另一个长轮询请求,直到有更多数据为止。我相信这就是 gmail 使用的方法。

这就是长轮询的要点,您必须进行一些修改,因为大多数浏览器如果长时间没有返回,将使 ajax 请求过期,因此如果 ajax 请求超时,客户端必须发出另一个请求(但是超时通常为一分钟或更长时间)。但这是主要思想。

服务器端的实现远比客户端复杂(客户端只需要几行js)。

The problem with periodic refresh is that while it is good for some things that aren't too time critical, like e-mail fetching, it is not instantaneous. Therefore you wouldn't want to use it for something like chat where waiting even five seconds for a response is too long. You could decrease the polling interval and make a request once a second or even half second, but then you'd quickly overload your browser and waste resources.

One solution for this is to use a technique called ajax long polling (known by others as 'comet' or 'reverse ajax'). With this technique, the browser makes a long duration ajax request, one that does not return until there is new data available. That request sits on the server (you need to be running special server side software to handle this in a scalable manner, but you could hack something together with php as a demonstration) until new data is available at which point it returns to the client with the new data. When the client receives the data it makes another long polling request to sit at the server until there is more data. This is the method that gmail uses, I believe.

That is the gist of long polling, you have to make a couple of modifications because most browsers will expire an ajax request if it does not return after a long time, so if the ajax request times out the client has to make another request (but the timeout is usually on the interval of a minute or longer). But this is the main idea.

The server side implementation of this is far more complicated than the client side (client side requires just a few lines of js).

送你一个梦 2024-10-24 14:18:24

虽然我不确定 gmail 的具体实现,但 AjaxPatterns 站点对他们所谓的定期刷新有一个很好的概述:--> http://ajaxpatterns.org/Periodic_Refresh。我一直将这种风格称为心跳

他们的解决方案的要点是:

浏览器定期发出XMLHttpRequest Call来获取新信息,例如每五秒调用一次。该解决方案利用浏览器的事件调度功能来提供一种让用户了解最新更改的方法。

它们包括一些实际示例的链接和一些示例代码。

While I'm not sure of the exact gmail implemention, the AjaxPatterns site has a good overview of what they dub a Periodic Refresh: --> http://ajaxpatterns.org/Periodic_Refresh. I've always just referred to the style as a heartbeat.

The gist of their solution is:

The browser periodically issues an XMLHttpRequest Call to gain new information, e.g. one call every five seconds. The solution makes use of the browser's Event Scheduling capabilities to provide a means of keeping the user informed of latest changes.

They include some links to real-world examples and some sample code.

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