Skip to content

Commit da07eb5

Browse files
committed
更新:采用原始的异步请求.
1 parent 5525cf7 commit da07eb5

File tree

4 files changed

+53
-34
lines changed

4 files changed

+53
-34
lines changed

WebApp/default/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
<!--[if lt IE 9]>
88
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
99
<![endif]-->
10-
<script type="text/javascript" src="http://localhost/jquery.min.js"></script>
1110
</head>
1211
<body>
1312
<h1>This the default page,you can find this page at ./WebApp/default/index.html</h1>

core/Ajax.php

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,63 @@
11
<script type="text/javascript">
2-
function getT(){
3-
var D = new Date();
4-
var t = String(D.getTime());
5-
t = Number(t.substr(0, 10));
6-
return t;
7-
}
8-
function poll(t){
9-
$.ajax({
10-
type: "GET",
11-
url: <?php $request_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
12-
echo '"'.substr($request_url, 0, stripos($request_url,'index.php')).'reload.php"';?>,
13-
data: {time:t},
14-
async: true,
15-
timeout: 60*1000,//60秒后服务器没有返回就重新发起
16-
success: function(msg){
17-
(typeof msg !== 'object') ? jsonObj = JSON.parse(msg) : jsonObj = msg ;
2+
var autoReload = function() {
3+
var request = null;
4+
var timeOut = 60*1000;//60s超时
5+
6+
function getT(){
7+
var D = new Date();
8+
var t = String(D.getTime());
9+
t = Number(t.substr(0, 10));
10+
return t;
11+
}
12+
13+
function createRequest() {
14+
if(window.XMLHttpRequest) {
15+
request = new XMLHttpRequest();
16+
} else if(window.ActiveXobject){
17+
try {
18+
request = new ActiveXObject("Microsoft.XMLHttp");
19+
} catch(e) {
20+
request = null;
21+
}
22+
}
23+
if(request == null)
24+
alert('你的浏览器不支持异步请求!');
25+
}
26+
27+
function sendRequest() {
28+
createRequest();
29+
var url = <?php $request_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
30+
echo '"'.substr($request_url, 0, stripos($request_url,'index.php')).'reload.php"';?>;
31+
request.open('GET',url + '?time=' + getT(),true);
32+
request.onreadystatechange = processRequest;
33+
request.send(null);
34+
setTimeout(abortRequest, timeOut);//超时重试
35+
}
36+
37+
function processRequest() {
38+
if(request.readyState == 4 && request.status == 200) {
39+
//请求结果已经返回
40+
var msg = request.responseText;
41+
(typeof msg !== 'object') ? jsonObj = JSON.parse(msg) : jsonObj = msg;
1842
if (1 === jsonObj.status){
19-
// alert("有新消息!"+msg.data);
2043
console.log("文件已经改动,可以刷新页面!");
21-
poll(getT());
2244
window.location.reload();//刷新页面
2345
} else {
2446
console.log("没有任何改动!再次建立查询...");
2547
console.log("msg=" + jsonObj.status + ",intime = "+ jsonObj.intime+"s");
26-
poll(getT());
27-
}
28-
},
29-
error: function (XMLHttpRequest, textStatus, errorThrown){
30-
if("timeout" === textStatus){
31-
// alert("超时啦!");
32-
console.log("等待超时,重新连接中...");
33-
poll(getT());
48+
sendRequest();
3449
}
3550
}
36-
}, "JSON");
51+
}
52+
function abortRequest() {
53+
console.log('查询超时!重新开始查询...');
54+
request.abort();
55+
sendRequest();
56+
}
57+
this.start = function() {//开始执行
58+
sendRequest();
59+
}
3760
}
38-
$(function(){
39-
poll(getT());
4061

41-
});
42-
</script>
62+
window.onload = (new autoReload()).start();
4363
</script>

core/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function rander()
3333
$page = $callHook['page'];
3434
if(file_exists(ROOT.DS.'WebApp'.DS.$app.DS.$page)) {
3535
include(ROOT.DS.'WebApp'.DS.$app.DS.$page);
36-
include (ROOT.DS.'core'.DS.'Ajax.php');//加入ajax请求
36+
include (ROOT.DS.'core'.DS.'Ajax2.php');//加入ajax请求
3737
} else {
3838
//show file not find
3939
echo "<h1>No such file or dirrectory</h1>";

data/default.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"dir":"default\\css\\style.css","md5":"d41d8cd98f00b204e9800998ecf8427e"},{"dir":"default\\index.html","md5":"2e9c785bf2e17e3578fe6e7a901fdd73"},{"dir":"default\\info.html","md5":"3218754a0679d3ae3bc63e854844db80"},{"dir":"default\\js\\default.js","md5":"d41d8cd98f00b204e9800998ecf8427e"},{"dir":"default\\js\\jquery.js","md5":"e1288116312e4728f98923c79b034b67"}]
1+
[{"dir":"default\\css\\style.css","md5":"d41d8cd98f00b204e9800998ecf8427e"},{"dir":"default\\index.html","md5":"b489d63579201a7f058a52889d60b57d"},{"dir":"default\\info.html","md5":"3218754a0679d3ae3bc63e854844db80"},{"dir":"default\\js\\default.js","md5":"d41d8cd98f00b204e9800998ecf8427e"},{"dir":"default\\js\\jquery.js","md5":"e1288116312e4728f98923c79b034b67"}]

0 commit comments

Comments
 (0)