Skip to content

Commit e0992bc

Browse files
committed
更好的轮询的机制,解决应用目录下的files加载问题.
1 parent b5640be commit e0992bc

File tree

6 files changed

+34
-16
lines changed

6 files changed

+34
-16
lines changed

.htaccess

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ RewriteEngine On
1616
#RewriteRule ^(.*)$ - [E=BASE:%1]
1717
#RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
1818

19-
RewriteRule (/?[^\/]*\/css\/)(.*\.(jpg|jpeg|png|gif|tif|js|css))$ WebApp$1$2
19+
RewriteRule (index.php)(.*.(jpg|jpeg|png|gif|tif|js|css))$ WebApp/$2

core/Ajax.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ function sendRequest() {
2828
createRequest();
2929
var url = <?php $request_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
3030
echo '"'.substr($request_url, 0, stripos($request_url,'index.php')).'reload.php"';?>;
31-
request.open('GET',url + '?time=' + getT(),true);
31+
var app = "<?php global $app; echo $app;?>";
32+
request.open('GET',url + '?time=' + getT() + '&app=' + app,true);
3233
request.onreadystatechange = processRequest;
3334
request.send(null);
3435
setTimeout(abortRequest, timeOut);//超时重试
@@ -38,6 +39,7 @@ function processRequest() {
3839
if(request.readyState == 4 && request.status == 200) {
3940
//请求结果已经返回
4041
var msg = request.responseText;
42+
console.log(msg);
4143
(typeof msg !== 'object') ? jsonObj = JSON.parse(msg) : jsonObj = msg;
4244
if (1 === jsonObj.status){
4345
console.log("文件已经改动,可以刷新页面!");

core/bootstrap.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,26 @@
44
require_once (ROOT . DS . 'core' . DS .'check.php');
55

66

7+
78
function callHook()
89
{
9-
global $url;
10+
global $url;//全局url
11+
global $app;
12+
global $page;
13+
14+
$uri = $_SERVER['REQUEST_URI'];
15+
$start = stripos($uri, 'index.php');
16+
if($start > 0)
17+
$url = substr($uri, $start + 10);
18+
1019
$urlArray = array();
1120
$urlArray = explode("/", $url);
1221
// echo '<br/>';
1322
// var_dump($urlArray);
1423
if($urlArray[0] != '') {
1524
$app = $urlArray[0];
1625
} else {
17-
$app = "default";//默认为default
26+
// $app = "default";//默认为default
1827
}
1928
array_shift($urlArray);//将数组开头的单元移出数组
2029
if(count($urlArray) != 0) {

core/check.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ function log_file($dir, $app)
4848
function is_change($app)
4949
{
5050
$dir = ROOT.DS.'WebApp'.DS.$app;
51+
52+
if(!file_exists('data/'.$app.'.json')) {//文件不存在
53+
log_file($dir, $app);//创建一个新的记录
54+
return true;
55+
}
56+
5157
$files = get_filenamesbydir($dir);
5258
$info = array();
5359
foreach ($files as $value) {

index.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
define('ROOT', dirname(__FILE__));
77

88
// echo substr($_SERVER['REQUEST_URI'], 0, stripos($_SERVER['REQUEST_URI'],'index.php')),$_SERVER['REQUEST_URI'];
9-
$uri = $_SERVER['REQUEST_URI'];
10-
$start = stripos($uri, 'index.php');
11-
if($start > 0)
12-
$url = substr($uri, $start + 10);
13-
else
14-
$url = '';
9+
$url = '';//全局变量
10+
$app = '';
11+
$page = '';
1512

1613
require_once(ROOT . DS. 'core' . DS . 'bootstrap.php');
1714
rander();//加载页面

reload.php

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

0 commit comments

Comments
 (0)