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