Long Polling

Hold a connection on the server side, do not return immediately, until there is data, this is the principle of long connection technology 

The key to the long connection technology is to hold an HTTP request until there is new data to respond to the request, and then the client automatically initiates a long connection request again.

So how do you hold a request? The code on the server side might look like this

 

<? php
 set_time_limit (0);   // This sentence is very important, so as not to run timeout 
while ( true ) {
     if (hasNewMessage()) {
         echo json_encode(getNewMessage());
         break ;
    }
    usleep (100000);       // Avoid too frequent queries 
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325073967&siteId=291194637