How to implement Ajax long polling in PHP

Source: Internet
Author: User
Tags usleep

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!

Related Article

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.