Tag: Stat One URL does not have Help alert string OSC response
Simple description:
Comet is a server push implemented with Ajax, there are two ways to implement comet, long polling and streaming, where only long polling is implemented.
Long polling process: The page initiates a server request, and the server keeps the connection open until there is data returned. After the data is returned, the browser closes the connection and then initiates another server request. This process is maintained continuously during page opening.
This approach saves bandwidth, and recursive requests (in order) are much better than normal polling disorder.
Testpush.html, the contents are as follows
Simple description:
Comet is a server push implemented with Ajax, there are two ways to implement comet, long polling and streaming, where only long polling is implemented.
Long polling process: The page initiates a server request, and the server keeps the connection open until there is data returned. After the data is returned, the browser closes the connection and then initiates another server request. This process is maintained continuously during page opening.
This approach saves bandwidth, and recursive requests (in order) are much better than normal polling disorder.
Testpush.html, the contents are as follows
?
12345678910111213141516171819202122232425262728293031323334353637383940 |
<
html
>
<
head
>
<
meta
http-equiv
=
"pragma"
content
=
"no-cache"
>
<
meta
http-equiv
=
"cache-control"
content
=
"no-cache"
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=utf-8"
/>
<
script
type
=
"text/javascript"
src
=
"jquery.min.js"
></
script
>
<
script
type
=
"text/javascript"
>
$(function () {
(function longPolling() {
alert(Date.parse(new Date())/1000);
$.ajax({
url: "testPush1111.php",
data: {"timed": Date.parse(new Date())/1000},
dataType: "text",
timeout: 5000,//5秒超时,可自定义设置
error: function (XMLHttpRequest, textStatus, errorThrown) {
$("#state").append("[state: " + textStatus + ", error: " + errorThrown + " ]<
br
/>");
if (textStatus == "timeout") { // 请求超时
longPolling(); // 递归调用
} else { // 其他错误,如网络错误等
longPolling();
}
},
success: function (data, textStatus) {
$("#state").append("[state: " + textStatus + ", data: { " + data + "} ]<
br
/>");
if (textStatus == "success") { // 请求成功
longPolling();
}
}
});
})();
});
</
script
>
</
head
>
<
body
>
<
div
id
=
"state"
></
div
>
</
body
>
</
html
>
|
testpush.php, the contents are as follows
Test analysis
There are several situations:
1. Successful return, status code 200, and then start the long connection again
2. Timeout, cancel (canceled) This long connection, and then start the long connection again
3. Long connection Waiting (pending), server response
testpush.php, the contents are as follows
?
123456789101112131415161718 |
<?php
if
(!
$_GET
[
‘timed‘
])
exit
();
date_default_timezone_set(
"PRC"
);
set_time_limit(0);
//无限请求超时时间
$timed
=
$_GET
[
‘timed‘
];
while
(true) {
sleep(3);
// 休眠3秒,模拟处理业务等
$i
= rand(0,100);
// 产生一个0-100之间的随机数
if
(
$i
> 20 &&
$i
< 56) {
// 如果随机数在20-56之间就视为有效数据,模拟数据发生变化
$responseTime
= time();
// 返回数据信息,请求时间、返回数据时间、耗时
echo
(
"result: "
.
$i
.
", response time: " .
$responseTime
.
", request time: "
.
$timed
.
", use time: "
. (
$responseTime
-
$timed
));
exit
();
}
else
{
// 模拟没有数据变化,将休眠 hold住连接
sleep(13);
exit
();
}
}
|
Test analysis
There are several situations:
1. Successful return, status code 200, and then start the long connection again
2. Timeout, cancel (canceled) This long connection, and then start the long connection again
3. Long connection Waiting (pending), server response
PHP JavaScript Comet