Just network about polling knowledge, must get their own here to do a backup!
In fact, before using Ajax polling to do a timely update of data, just did not know that is polling.
First of all, when do we think of using polling technology?
In general, the most is timely information updates, such as a mall activity, the number of participants in real-time updates, but also someone used to do chat room, but ha, polling technical issues are many, frequent requests for the server, the server will put IP to you on the non-whitelist, so you can not request the server. So in a timely manner I still recommend using WebSocket to establish a long connection.
Next, Xiao Yang used a teacher's exact words to explain AJAX polling technology:
One, Ajax polling-"timed through the AJAX query server"
Ajax polling--"timed through the AJAX query server."
Concept:
Polling (polling): The client sends an AJAX request at a scheduled time, like a server, and returns the response information and closes the connection immediately after the request is received.
Seeing is believing, a piece of code, I believe you see it.
In order to let the students understand, I used the simplest way to achieve, the students understand the principle can be self-derived:
reception.html//Front-end code
<HTML><Head> <title></title> <Scriptsrc= "Http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></Script></Head><Body></Body><Scripttype= "Text/javascript"> vargetting={URL:'server.php', DataType:'JSON', Success:function(res) {Console.log (res);}};//the key here, Ajax timed access to the server, and constantly get data, here is a 1-second request once. Window.setinterval (function() {$.ajax (getting)}, +);</Script></HTML>
Service side.
server_polling.php
<?PHP$pdo=NewPDO (' mysql:dbname=test;host=127.0.0.1 ', ' root ', ' root ');$resource=$pdo->query (' select * from T1 ');$result=$resource-Fetchall ();if($result) { //exits Data Print_r(Json_encode (Array(' success ' = ' presence data '))); Exit();}Print_r(Json_encode (Array(' failed ' = ' no data present ')));Exit();?>
Above is the most basic and simplified Ajax polling. Determine if the T1 table has data and execute the corresponding output. In the actual project, the query statement will be based on the actual needs to be determined.
The key point of Ajax polling is "window.setinterval (function () {$.ajax (getting)},1000)," which is the "client sends Ajax requests at timed times like a server" at the beginning of the article.
Let's explain it in figures:
We can see that the client is in accordance with the specified time (this time is set by you, the default here is 1 seconds) like the server to send a request, after the previous request is completed, regardless of the results returned, a second after the next request will be issued. This is called Ajax polling.
Use pseudo-code to represent:
<? PHP // Ajax Polling while (true) { echo' sends a request '; Sleep // this request has been sent, and after a second break continue to request }?>
The key point is that the client needs to set a timer through JS, according to the request of constant time.
Easy, in fact, it is not so difficult, many times, just we ourselves are frightened by the new technology
Well, let's take a look at Ajax Long polling, which is an upgraded version of Ajax polling.
Two, Ajax long polling (long polling)
Ajax long polling is an upgraded version of Ajax polling, with a number of modifications on both the client and server side, making it less expensive and faster.
"Uninterrupted access to the server through Ajax".
reception.html//Client
<HTML><Head> <title></title> <Scriptsrc= "Http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></Script></Head><Body></Body><Scripttype= "Text/javascript"> //front-end Ajax continues to invoke the server, called the Ajax polling technology vargetting={URL:'server.php', DataType:'JSON', Success:function(res) {Console.log (res); $.ajax (getting); //The key here is to request Ajax again inside the callback function} //when the request takes too long (the default is 60 seconds), the Ajax long polling is called againError:function(res) {$.ajax ($getting); }};$.ajax (getting);</Script></HTML>
server.php://server
<?PHP//This AJAX request time never expiresSet_time_limit(0);$pdo=NewPDO (' mysql:dbname=test;host=127.0.0.1 ', ' root ', ' root ');$resource=$pdo->query (' select * from T1 ');$result=$resource-Fetchall (); while(true) { if($result) { //exits Data Print_r(Json_encode (Array(' success ' = ' There is data, return '))); Exit();//output data, exit. The client then continues to initiate the request without interruption } //The data does not exist and continues the loop. }?>
The essence of long polling is the Ajax callback function, which continues to invoke the Ajax request again (the continuous principle is here, and is called again immediately after successful return):
$.ajax (getting);
Here is a schematic of the Ajax long polling:
There are two requests (the request in this article by default is Ajax), the first time to return the results, and then immediately send a second request without interruption, but the second request did not get data, so the request has not returned (is hung on the server, but as long as the data will return), If the second request returns data, the third request is immediately emitted, a technique called Ajax long polling.
This is how it is represented by pseudo-code:
<? PHP // The client initiates the request via Ajax if (' client sends request ' && ' server has data can return ') { echo ' returns data to client '; Echo ' The client continues to initiate the request through Ajax, and then continues with the if judgment ';} Else { echo' has no data to return, perform an if judgment once again ';}? >
The key point is that the second request, after the end of the previous request, is immediately uninterrupted, called the Ajax Long polling
Ajax polling and long polling