实时更新如何工作? [英] How do real time updates work?

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

问题描述

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

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

我想知道怎么做这些实时更新有效吗?我只是在寻找一般鸟类的观点。我怀疑JS无法每隔X秒调用服务器进行更新,然后将其附加到< ul> 。从服务器发送的通知是否会提取更多内容?

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 <ul>. Is a notification sent from the server went to pull more content?

如果有一篇简单的文章通过演示来解释这个问题会不会很好?

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

推荐答案

Stack Overflow正在使用 Web Sockets 实时更新。如果您查看源代码(2012源代码),您会看到:

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:

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

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

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

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

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.

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

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

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

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