Facebook,Gmail如何发送实时通知? [英] How does facebook, gmail send the real time notification?

查看:95
本文介绍了Facebook,Gmail如何发送实时通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了有关此主题的一些帖子,答案是彗星,反向ajax,http流,服务器推送等。

I have read some posts about this topic and the answers are comet, reverse ajax, http streaming, server push, etc.

Gmail上的传入邮件通知如何工作?

如何使用GMail聊天能够在没有客户端交互的情况下发出AJAX请求吗?

我想知道是否有任何代码引用可以用来编写一个非常简单的示例。许多帖子或网站只是在谈论这项技术。很难找到完整的示例代码。而且,似乎可以使用多种方法来实现彗星,例如隐藏的IFrame,XMLHttpRequest。我认为,使用XMLHttpRequest是更好的选择。您如何看待不同方法的利弊? Gmail使用的是哪一个?

I would like to know if there are any code references that I can follow to write a very simple example. Many posts or websites just talk about the technology. It is hard to find a complete sample code. Also, it seems many methods can be used to implement the comet, e.g. Hidden IFrame, XMLHttpRequest. In my opinion, using XMLHttpRequest is a better choice. What do you think of the pros and cons of different methods? Which one does Gmail use?

我知道它需要在服务器端和客户端都需要这样做。
是否有PHP和Javascript示例代码?

I know it needs to do it both in server side and client side. Is there any PHP and Javascript sample code?

推荐答案

Facebook这样做的方式非常有趣。

The way Facebook does this is pretty interesting.

执行此类通知的一种常见方法是,以给定的时间间隔(也许每隔几秒钟)轮询服务器上的脚本(使用AJAX),以检查是否有发生了但是,这可能会占用大量网络资源,并且您经常会提出毫无意义的请求,因为什么都没发生。

A common method of doing such notifications is to poll a script on the server (using AJAX) on a given interval (perhaps every few seconds), to check if something has happened. However, this can be pretty network intensive, and you often make pointless requests, because nothing has happened.

Facebook的操作方式是使用彗星方法,而不是轮询每隔一段时间,一次民意调查完成后,就会发出另一次民意调查。但是,对服务器上脚本的每个请求都具有非常长的超时,并且服务器仅在发生某些情况时才响应该请求。如果您在Facebook上打开Firebug的控制台标签,并且请求脚本的请求可能需要几分钟,就会看到这种情况。确实是非常巧妙的,因为此方法可立即减少请求数量和发送频率。现在,您有效地有了一个事件框架,该事件框架允许服务器触发事件。

The way Facebook does it is using the comet approach, rather than polling on an interval, as soon as one poll completes, it issues another one. However, each request to the script on the server has an extremely long timeout, and the server only responds to the request once something has happened. You can see this happening if you bring up Firebug's Console tab while on Facebook, with requests to a script possibly taking minutes. It is quite ingenious really, since this method cuts down immediately on both the number of requests, and how often you have to send them. You effectively now have an event framework that allows the server to 'fire' events.

在这之后,就从这些民意调查返回的实际内容而言,它是一个JSON响应,其中似乎包含事件列表以及有关事件的信息。

Behind this, in terms of the actual content returned from those polls, it's a JSON response, with what appears to be a list of events, and info about them. It's minified though, so is a bit hard to read.

就实际技术而言,AJAX是解决问题的方法,因为您可以控制请求超时,并且许多其他事情。我建议(这里是堆栈溢出老套)使用jQuery来做AJAX,它将消除很多交叉兼容性问题。就PHP而言,您可以简单地在PHP脚本中轮询事件日志数据库表,并且仅在发生某些情况时才返回到客户端?我希望有很多方法可以实现此目的。

In terms of the actual technology, AJAX is the way to go here, because you can control request timeouts, and many other things. I'd recommend (Stack overflow cliche here) using jQuery to do the AJAX, it'll take a lot of the cross-compability problems away. In terms of PHP, you could simply poll an event log database table in your PHP script, and only return to the client when something happens? There are, I expect, many ways of implementing this.

实施:

服务器端:

PHP中似乎有一些彗星库的实现,但老实说,它确实非常简单,可能类似于以下伪代码:

There appear to be a few implementations of comet libraries in PHP, but to be honest, it really is very simple, something perhaps like the following pseudocode:

while(!has_event_happened()) {
   sleep(5);
}

echo json_encode(get_events());




  • has_event_happened函数只会检查是否发生了什么事件表之类的东西,然后get_events函数将返回表中新行的列表?

    • The has_event_happened function would just check if anything had happened in an events table or something, and then the get_events function would return a list of the new rows in the table? Depends on the context of the problem really.

      别忘了更改您的PHP最大执行时间,否则它将提前超时!

      Don't forget to change your PHP max execution time, otherwise it will timeout early!

      客户端:

      看看做Comet的jQuery插件互动:

      Take a look at the jQuery plugin for doing Comet interaction:

      • Project homepage: http://plugins.jquery.com/project/Comet
      • Google Code: https://code.google.com/archive/p/jquerycomet/ - Appears to have some sort of example usage in the subversion repository.

      也就是说,该插件似乎增加了一定的复杂性,它在客户端上确实非常简单,也许(使用jQuery)如下: / p>

      That said, the plugin seems to add a fair bit of complexity, it really is very simple on the client, perhaps (with jQuery) something like:

      function doPoll() {
         $.get("events.php", {}, function(result) {
            $.each(result.events, function(event) { //iterate over the events
                //do something with your event
            });
            doPoll(); 
            //this effectively causes the poll to run again as
            //soon as the response comes back
         }, 'json'); 
      }
      
      $(document).ready(function() {
          $.ajaxSetup({
             timeout: 1000*60//set a global AJAX timeout of a minute
          });
          doPoll(); // do the first poll
      });
      

      整个过程很大程度上取决于现有架构的组合方式。

      The whole thing depends a lot on how your existing architecture is put together.

      这篇关于Facebook,Gmail如何发送实时通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆