Skip to content

Commit 5525cf7

Browse files
committed
finish auto reload while files had changed
1 parent b7be09c commit 5525cf7

File tree

9 files changed

+108
-21
lines changed

9 files changed

+108
-21
lines changed

.htaccess

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
RewriteEngine On
3+
# The following rule tells Apache that if the requested filename
4+
# exists, simply serve it.
5+
RewriteCond %{REQUEST_FILENAME} -s [OR]
6+
RewriteCond %{REQUEST_FILENAME} -l [OR]
7+
RewriteCond %{REQUEST_FILENAME} -d
8+
RewriteRule ^.*$ - [NC,L]
9+
# The following rewrites all other queries to index.php. The
10+
# condition ensures that if you are using Apache aliases to do
11+
# mass virtual hosting, the base path will be prepended to
12+
# allow proper resolution of the index.php file; it will work
13+
# in non-aliased environments as well, providing a safe, one-size
14+
# fits all solution.
15+
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::$
16+
RewriteRule ^(.*)$ - [E=BASE:%1]
17+
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

WebApp/default/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
<head>
44
<meta charset="UTF-8">
55
<title>Exercise</title>
6-
<link rel="stylesheet" type="text/css" href="css/styles.css">
6+
<link rel="stylesheet" type="text/css" href="css/style.css">
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>
1011
</head>
1112
<body>
1213
<h1>This the default page,you can find this page at ./WebApp/default/index.html</h1>
14+
<p>h</p>
15+
<script type="text/javascript"></script>
1316
</body>
1417
</html>

WebApp/default/js/jquery.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/Ajax.php

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,43 @@
11
<script type="text/javascript">
2-
var div = document.createElement('div');
3-
document.body.appendChild(div);
4-
function time(){
5-
var date = new Date();
6-
div.innerHTML = date.getHours() +' : ' + date.getMinutes() +' : ' + date.getSeconds();
7-
setTimeout(time,900);
8-
}
9-
time();
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 ;
18+
if (1 === jsonObj.status){
19+
// alert("有新消息!"+msg.data);
20+
console.log("文件已经改动,可以刷新页面!");
21+
poll(getT());
22+
window.location.reload();//刷新页面
23+
} else {
24+
console.log("没有任何改动!再次建立查询...");
25+
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());
34+
}
35+
}
36+
}, "JSON");
37+
}
38+
$(function(){
39+
poll(getT());
40+
41+
});
42+
</script>
1043
</script>

core/bootstrap.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
require_once (ROOT . DS . 'config' . DS .'config.php');
44
require_once (ROOT . DS . 'core' . DS .'check.php');
55

6+
67
function callHook()
78
{
89
global $url;
9-
1010
$urlArray = array();
1111
$urlArray = explode("/", $url);
12-
echo '<br/>';
13-
var_dump($urlArray);
12+
// echo '<br/>';
13+
// var_dump($urlArray);
1414
if($urlArray[0] != '') {
1515
$app = $urlArray[0];
1616
} else {
@@ -23,17 +23,23 @@ function callHook()
2323
$page = "index.html";//默认为index.html
2424
}
2525
array_shift($urlArray);
26+
return array('app'=>$app,'page'=>$page);
27+
}
2628

29+
function rander()
30+
{
31+
$callHook = callHook();
32+
$app = $callHook['app'];
33+
$page = $callHook['page'];
2734
if(file_exists(ROOT.DS.'WebApp'.DS.$app.DS.$page)) {
2835
include(ROOT.DS.'WebApp'.DS.$app.DS.$page);
29-
include (ROOT.DS.'core'.DS.'Ajax.php');
36+
include (ROOT.DS.'core'.DS.'Ajax.php');//加入ajax请求
3037
} else {
3138
//show file not find
3239
echo "<h1>No such file or dirrectory</h1>";
3340
}
3441
}
3542

36-
callHook();
37-
print_files(ROOT.DS.'WebApp');
43+
// print_files(ROOT.DS.'WebApp');
3844
//log_file(ROOT.DS.'WebApp', 'default');
39-
is_change('default');
45+
// is_change('default');

core/check.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ function is_change($app)
5959
$content = fgets($handle);
6060
fclose($handle);
6161
// var_dump($file_info,$content);
62-
if(!strcmp($file_info, $content)) {
63-
echo 'no change';//
62+
if(!strcmp($file_info, $content)) {//比较文件信息
63+
// echo 'no change';
64+
return false;
6465
} else {
65-
echo 'had changed';
66-
// log_file($dir, $app);
66+
// echo 'had changed';
67+
log_file($dir, $app);
68+
return true;
6769
}
6870
}

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":"4066d251c13ec1713ffe6acb944a3f20"},{"dir":"default\\info.html","md5":"3218754a0679d3ae3bc63e854844db80"},{"dir":"default\\js\\default.js","md5":"d41d8cd98f00b204e9800998ecf8427e"}]
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"}]

index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
define('DS', DIRECTORY_SEPARATOR);
66
define('ROOT', dirname(__FILE__));
77

8+
// echo substr($_SERVER['REQUEST_URI'], 0, stripos($_SERVER['REQUEST_URI'],'index.php')),$_SERVER['REQUEST_URI'];
89
$uri = $_SERVER['REQUEST_URI'];
910
$start = stripos($uri, 'index.php');
1011
if($start > 0)
@@ -13,3 +14,4 @@
1314
$url = '';
1415

1516
require_once(ROOT . DS. 'core' . DS . 'bootstrap.php');
17+
rander();//加载页面

reload.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,23 @@
11
<?php
2+
define('DS', DIRECTORY_SEPARATOR);
3+
define('ROOT', dirname(__FILE__));
4+
require_once (ROOT . DS . 'core' . DS .'bootstrap.php');
5+
$callHook = callHook();
6+
set_time_limit(0);//设定执行时间,0为无限
7+
$startTime = time();//当前时间戳
8+
$flag = false;
9+
while(time() - $startTime < 60) {//60秒内检查文件是否改动
10+
if(is_change($callHook['app'])) {
11+
$flag = true;
12+
break;
13+
}
14+
sleep(1);//间隔1秒检查
15+
}
16+
// sleep(rand(1,10));
17+
if($flag){
18+
$sendTime = $_GET['time'];
19+
echo json_encode(array('status'=>1,'intime'=>time()-$sendTime));
20+
} else {
21+
$sendTime = $_GET['time'];
22+
echo json_encode(array('status'=>0,'intime'=>time()-$sendTime));
23+
}

0 commit comments

Comments
 (0)