What is polling polling means that the client periodically sends Ajax requests to the server. Upon receiving the request, the server immediately returns a response message and closes the connection. The simple implementation of polling is just to say the definition, seems to be very fuzzy, the code is more intuitive, here is a simple implementation of polling: front-end code (jquery Ajax as an example, of course, you can also through pure JS way)

[JavaScript]

Plain text view
Copy the code

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
<html>
<head>

<title> Haorooms Blog polling test case </title>

<script src=
"http://www.haorooms.com/theme/assets/js/jquery.js"
></script>
</head>
<body>
<script type=
"text/javascript"
>

var
getting = {

url:
'server.php'
.

dataType:
'json'
.

success:
function
(res) {

console.log(res);
}
};
//Ajax makes periodic calls to the server to continuously fetch data, in this case once a second.
window.setInterval(
function
(){$.ajax(getting)},1000);
</script>
</body>
</html>

Server side, PHP as an example:

[JavaScript]

Plain text view
Copy the code

?
01
02
03
04
05
06
07
08
09
10
11
12
<? php
$pdo =
new
PDO(
'mysql:dbname=haoroomstest; The host = 127.0.0.1 '
.
'root'
.
'root'
);
$resource = $pdo->query(
'select * from haorooms'
);
$result = $resource->fetchall();
if
($result) {

// Data exists

print_r(json_encode(array(
'success'
= >
'Existing data'
)));

exit();
}
print_r(json_encode(array(
'failed'
= >
'No data exists'
)));
exit();
? >

As you can see, after the previous request is completed, the next request will be made a second later, regardless of whether the result is returned. This is called Ajax polling. As you can see from this case, there are performance issues and a lot of stress on the server. But this is one way to implement polling. What is long polling? A client sends an Ajax request to the server. The server receives the request and holds the connection until it receives a new message and closes the connection. From the above description, we can see that long polling itself is not a true push technique, but just a variation of the traditional polling technique. However, it can simulate a push mechanism when real push technology is not available (other push methods, see below). Long polling simple implementation directly on the code to show it! The front-end code is as follows:

[JavaScript]

Plain text view
Copy the code

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<html>
<head>

<title> Haorooms blog long Polling test case </title>

<script src=
"http://www.haorooms.com/theme/assets/js/jquery.js"
></script>
</head>
<body>
<script type=
"text/javascript"
>

// The front-end Ajax continually calls the server, a technique called Ajax polling

var
getting = {

url:
'server.php'
.

dataType:
'json'
.

success:
function
(res) {

console.log(res);

$.ajax(getting);
// The key here is that Ajax is requested again within the callback function
}

// When the request takes too long (60 seconds by default), ajax long polling is called again

error:
function
(res){

$.ajax($getting);

}
};
$.ajax(getting);
</script>
</body>
</html>

Server.php on the server side:

[JavaScript]

Plain text view
Copy the code

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
<? php
// This AJAX request time never expires
set_time_limit(0);
$pdo =
new
PDO(
'mysql:dbname=haoroomstest; The host = 127.0.0.1 '
.
'root'
.
'root'
);
$resource = $pdo->query(
'select * from haorooms'
);
$result = $resource->fetchall();
while
(
true
) {

if
($result) {

//exits data

print_r(json_encode(array(
'success'
= >
'Data present, return'
)));

exit();
// Output data, exit. The client then continues to make requests without interruption

}

// Data does not exist, continue loop.
}
? >

Through the above two cases, I believe you have a certain understanding of long polling and polling, the next section I will talk about long and short connections. The original post address: https://www.haorooms.com/post/ajax_longlunxun_dlx