index.html<script type="text/javascript">
<!--
var time =0;
var http_request=null;
function HttpRequest(){
if (window.ActiveXObject){
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}else if (window.XMLHttpRequest){
http_request = new XMLHttpRequest();
}
return http_request;
}
HttpRequest();
function autoSend(timestamp){
alert(timestamp);
var url = 'back.php';
http_request.open('POST', url);
http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
http_request.send('timestamp='+ timestamp +'&num='+Math.random());
http_request.onreadystatechange = CallBack;
}
function send(){
var msg = document.getElementById('msg').value;
document.getElementById('msg').value = '';
var url = 'back.php';
http_request.open('POST', url);
http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
http_request.send('msg='+msg+'&num='+Math.random());
}
function CallBack(){
if(http_request.readyState == 4) {
if (http_request.status == 200) {
var data = http_request.responseText;
alert(data);
var json = eval('('+ data +')');
document.getElementById('content').innerHTML += '<div>'+ json.msg +'</div>';
autoSend(json.timestamp);
}
}
}
window.onload = function(){
autoSend(0);
}
//-->
</script>
<div id="content"></div>
<input type="text" id="msg"><input type="button" value="Send" onclick="send()">
back.php
<?phpset_time_limit(0);
ob_end_clean();$file = dirname(__FILE__). '/data.txt';if(!empty($_REQUEST['msg'])){
file_put_contents($file, $_REQUEST['msg']);
die('Write');
}$lastmodif = isset($_REQUEST['timestamp']) ? $_REQUEST['timestamp'] : 0;
$currentmodif = filemtime($file);
while($currentmodif <= $lastmodif ){
sleep(2);
clearstatcache();
$currentmodif = filemtime($file);
}$response = array();
$response['msg'] = file_get_contents($file);
$response['timestamp'] = $currentmodif;
echo json_encode($response);
flush();
?>
<!--
var time =0;
var http_request=null;
function HttpRequest(){
if (window.ActiveXObject){
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}else if (window.XMLHttpRequest){
http_request = new XMLHttpRequest();
}
return http_request;
}
HttpRequest();
function autoSend(timestamp){
alert(timestamp);
var url = 'back.php';
http_request.open('POST', url);
http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
http_request.send('timestamp='+ timestamp +'&num='+Math.random());
http_request.onreadystatechange = CallBack;
}
function send(){
var msg = document.getElementById('msg').value;
document.getElementById('msg').value = '';
var url = 'back.php';
http_request.open('POST', url);
http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
http_request.send('msg='+msg+'&num='+Math.random());
}
function CallBack(){
if(http_request.readyState == 4) {
if (http_request.status == 200) {
var data = http_request.responseText;
alert(data);
var json = eval('('+ data +')');
document.getElementById('content').innerHTML += '<div>'+ json.msg +'</div>';
autoSend(json.timestamp);
}
}
}
window.onload = function(){
autoSend(0);
}
//-->
</script>
<div id="content"></div>
<input type="text" id="msg"><input type="button" value="Send" onclick="send()">
back.php
<?phpset_time_limit(0);
ob_end_clean();$file = dirname(__FILE__). '/data.txt';if(!empty($_REQUEST['msg'])){
file_put_contents($file, $_REQUEST['msg']);
die('Write');
}$lastmodif = isset($_REQUEST['timestamp']) ? $_REQUEST['timestamp'] : 0;
$currentmodif = filemtime($file);
while($currentmodif <= $lastmodif ){
sleep(2);
clearstatcache();
$currentmodif = filemtime($file);
}$response = array();
$response['msg'] = file_get_contents($file);
$response['timestamp'] = $currentmodif;
echo json_encode($response);
flush();
?>
问题点数:10分费办不讨好的事情谁做啊。