PHP implementation Ajax Long polling _php tutorial

Source: Internet
Author: User

PHP implementation Ajax Long polling


The traditional Ajax polling method, the customer service side at the user-defined time interval to the server to query the latest data. This kind of pull data takes a short time interval to ensure the accuracy of the data, but too short a time interval client 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



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

 
  "1", ' name ' = ' Xiaoyu ', ' text ' = $rand);                  echo Json_encode ($arr);                  Exit ();              }                            Server ($_post[' time ']*0.5) seconds to tell the Customer 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!

http://www.bkjia.com/PHPjc/989128.html www.bkjia.com true http://www.bkjia.com/PHPjc/989128.html techarticle PHP Implementation of Ajax long polling traditional Ajax polling, customer service at a user-defined time interval to the server to query the latest data. This way of pulling data takes a short time ...

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