热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

实时更新如何工作?-Howdorealtimeupdateswork?

Nowadaysrealtimeupdatesarecommoninmostpopularsiteswhichhaveheavyusages.现在,在大多数流行网站中

Now a days real time updates are common in most popular sites which have heavy usages.

现在,在大多数流行网站中,实时更新很常见。

  • StackExchange
  • Facebook
  • Twitter

I'm wondering how do these "real time updates" work? I'm just looking for a general bird's view perspective. I suspect that the JS can't be calling the server every X seconds for an update and then appending that to the

    . Is a notification sent from the server went to pull more content?

    我想知道这些“实时更新”是如何工作的?我只是在寻找一般鸟类的观点。我怀疑JS不能每隔X秒调用服务器进行更新,然后将其附加到

      。从服务器发送的通知是否会提取更多内容?

    Would be great if there is a simple how to article that explains this with a demo?

    如果有一个简单的文章如何通过演示来解释这个,那会很棒吗?

    3 个解决方案

    #1


    25  

    Stack Overflow is using Web Sockets for real time updates. If you take a look in the source code (2012 source code), you would see:

    Stack Overflow使用Web套接字进行实时更新。如果您查看源代码(2012源代码),您会看到:

    StackExchange.ready(function () {
        StackExchange.realtime.init('ws://sockets.ny.stackexchange.com');
        StackExchange.realtime.subscribeToInboxNotifications();
        StackExchange.realtime.subscribeToReputationNotifications('1');
    });
    

    But note that some Opera versions do not support WebSocket. (not until Opera 10.70)

    但请注意,某些Opera版本不支持WebSocket。 (直到Opera 10.70)

    However Facebook does not seem to be using Web Sockets, and I think they are just using simple XHR with a technique called long polling, which the server holds on to the connection until there is new information, and then respond to the request. If you open up the developer tools you can see that there is always one request which has a status of pending.

    然而,Facebook似乎没有使用Web套接字,我认为他们只是使用简单的XHR和一种称为长轮询的技术,服务器在有新信息之前保持连接,然后响应请求。如果打开开发人员工具,您会发现总有一个请求具有待处理状态。

    It is indeed, sending a request every ~60 seconds.

    确实,每隔约60秒发送一次请求。

    #2


    2  

    It seems that Twitter also uses simple XHR (1 minute intervals) for their "real time updates".

    Twitter似乎也使用简单的XHR(1分钟间隔)进行“实时更新”。

    #3


    0  

    Facebook uses long polling/Comet. So it makes a connection and waits for a response, if no response, then it times out and tries again. The timeout is around 40 secs. That's how it does most of the instant updating. However they use a combination of techniques. More on long polling here.

    Facebook使用长轮询/ Comet。所以它建立连接并等待响应,如果没有响应,则超时并再次尝试。超时约为40秒。这就是大多数即时更新的方式。然而,他们使用了一系列技术。更多关于长期投票的信息。

    http://en.wikipedia.org/wiki/Comet_(programming)


推荐阅读
author-avatar
张梦蒙4428
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有