Comet Reverse Ajax

Source: Internet
Author: User

One: HTTP protocol with technology-long link + chunked transfer----> Reverse Ajax

Reverse Ajax is also known as comet, Server push, and servers 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

The principle of split-field transmission is this:

123H \ r \ n

123H length of content transmission to the client ... \ r \ n

.......

41h\r\n

The browser continues to receive 41H-length content. \ r \ n

0\r\n (server says content is finished)

Combine with MySQL

<?php

Set_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 ');

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);
}

Comet 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.