HTTP protocol with chunked transfer, persistent connection and reverse Ajax

Source: Internet
Author: User

Web QQ has been used, which involves the technology, there is a reverse Ajax.

Reverse Ajax is also called Comet,server push, server push technology.

Application: web chat server, Sina Weibo online chat, Google Mail web chat, all useful.

Principle: Generally speaking, the characteristics of HTTP protocol, connection <-> disconnection.

When is the specific time to disconnect?

The server responds Content-length, and when it receives the content of the specified length, it is disconnected.

In the http1.1 protocol, you are allowed to not write content-length, such as when the length of the content to be sent is really unknown.

At this point, a special content-type:chunked is required



<?phpset_time_limit (0); Ob_start (); $pad = Str_repeat (", 4000); Echo $pad, ' <br/> '; Ob_flush (); flush ();  Send the resulting content to the browser immediately instead of waiting for the script to end. $conn = mysql_connect (' localhost ', ' root ', ' 111111 '), mysql_query (' Use test ') and while (1) {    $sql = ' select * from msg where flag = 0 ';    $rs = mysql_query ($sql, $conn);    $row = Mysql_fetch_assoc ($rs);    if (!empty ($row)) {        echo $pad, ' <br/> ';        echo $row [' content '], ' <br/> ';        mysql_query (' Update msg set flag=1 ');    }    Ob_flush ();    Flush ();  Send the resulting content to the browser immediately instead of waiting for the script to end.    Sleep (1);} /* Read the contents of the database so that it can be achieved, instant communication. Server-Side---uninterrupted--push message--to client */



HTTP protocol with chunked transfer, persistent connection and reverse Ajax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.