The traditional Ajax polling method, the customer service side at the user-defined time interval to the server to query the latest data. This method of pulling data takes a short time interval to ensure the accuracy of the data, but too short a time interval the customer sends multiple requests to the server in a short period of time.
Reverse Ajax, which is called long polling or comet. The server and customer service need to maintain a long-time request, which allows the server to return messages to the client when there is data.
This uses Ajax to request the data.php page to get the value of ' success ', requesting a time of 80 seconds. In these 80 seconds, if no return from the server ' success ' is kept in a connected state until there is a data return or ' success ' value of 0 to close the connection. The next request continues after the connection is closed.
Index.html
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Here is the infinite loop, the end condition of the loop is to get the return result to return the JSON data.
and accept the $_post[' time ' parameter to limit the cycle timeout and avoid excessive waste of resources. (browser close will not send a message to the server, use may have been looped down)
data.php
<?php if (Empty ($_post[' time ')) exit (); Set_time_limit (0);//Infinite Request Timeout time $i =0; while (true) { //sleep (1); Usleep (500000);//0.5 seconds $i + +; If you get the data, return the data to the customer service immediately, and end the request $rand =rand (1,999); if ($rand <=15) { $arr =array (' success ' = "1", ' name ' = ' Xiaoyu ', ' text ' + $rand); echo Json_encode ($arr); Exit (); } Server ($_post[' time ']*0.5) seconds to tell the customer service end no data if ($i ==$_post[' time ') { $arr =array (' success ' = ' 0 ', ' name ' = ' Xiaoyu ', ' text ' = $rand); echo Json_encode ($arr); Exit (); } } ? >
Run effect: In the graph can see the request time of no data reached 40S, in the request of 40S to obtain the data request closes. After closing, proceed to the next request!
The traditional Ajax polling method, the customer service side at the user-defined time interval to the server to query the latest data. This method of pulling data takes a short time interval to ensure the accuracy of the data, but too short a time interval the customer sends multiple requests to the server in a short period of time.
Reverse Ajax, which is called long polling or comet. The server and customer service need to maintain a long-time request, which allows the server to return messages to the client when there is data.
This uses Ajax to request the data.php page to get the value of ' success ', requesting a time of 80 seconds. In these 80 seconds, if no return from the server ' success ' is kept in a connected state until there is a data return or ' success ' value of 0 to close the connection. The next request continues after the connection is closed.
Index.html
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Here is the infinite loop, the end condition of the loop is to get the return result to return the JSON data.
and accept the $_post[' time ' parameter to limit the cycle timeout and avoid excessive waste of resources. (browser close will not send a message to the server, use may have been looped down)
data.php
<?php if (Empty ($_post[' time ')) exit (); Set_time_limit (0);//Infinite Request Timeout time $i =0; while (true) { //sleep (1); Usleep (500000);//0.5 seconds $i + +; If you get the data, return the data to the customer service immediately, and end the request $rand =rand (1,999); if ($rand <=15) { $arr =array (' success ' = "1", ' name ' = ' Xiaoyu ', ' text ' + $rand); echo Json_encode ($arr); Exit (); } Server ($_post[' time ']*0.5) seconds to tell the customer service end no data if ($i ==$_post[' time ') { $arr =array (' success ' = ' 0 ', ' name ' = ' Xiaoyu ', ' text ' = $rand); echo Json_encode ($arr); Exit (); } } ? >
Run effect: In the graph can see the request time of no data reached 40S, in the request of 40S to obtain the data request closes. After closing, proceed to the next request!