Facebook代码是如何工作的? [英] How does Facebook ticker work?

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

问题描述

大多数Facebook用PHP编写,但是有一些使用其他脚本语言的前端功能.

Most of Facebook is written in PHP but there are a few front end features that use other scripting languages.

代码(新闻源页面右上角的小框,显示最近的帖子等)

Ticker (the small box at the top right of the news feed page displaying recent posts etc etc):

我猜想AJAX参与其中,但是我想知道它是如何工作的.我在Flash中开发了类似的东西(但更基础),其中Flash每毫秒(以及实时)检查一次更新,但Facebook显然不使用Flash.

I'm guessing AJAX is involved in this but I was wondering how it all works. I've developed something similar (but more basic) in flash where flash checks every millisecond (as good as real time) for updates but Facebook clearly doesn't use flash for this.

我知道可以使用AJAX来回传递数据,但是它们将如何使其即时化?不断检查?

I know data can be passed back and forth with AJAX but how would they make it instant? Constantly checking?

想知道

推荐答案

他们正在使用长时间轮询

They are using long polling

  • 请求正在发送到服务器
  • 直到有新闻,请求才会关闭连接
  • 一旦有新闻,脚本将关闭连接并且新闻可见

长时间轮询的PHP脚本看起来像

Long polling PHP script can look like

$seconds = 1;
while($seconds < 60) { // browser can enforce one minute timeout
    $updates = get_updates();    //check for updates
    if ($updates) {
        echo $updates; // json encoded string
        die();
    }
    $seconds++;
    sleep(1);
}

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

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