php技术–php ajax服务器推技术的实现方法

内容纲要

在做实时信息输出的时候,我们很多时候会想到用ajax的setTimeOut方法,这个方法对于小的访问量来说没有什么问题,但是一旦访问量过大,页面过多就会对数据库造成很大的压力.这是我们常见的拉,相当于在每隔一段时间就会问一下服务器,你有没有新的消息让我显示?其中的道理已经是不言而喻了.

那么服务器推技术就应运而生了.他的原理相当于:当服务器中发现有新的消息的时候就会直接告诉客户端显示最新的消息.这样就节省了很多的资源.当然他也有瓶颈,如访问量大的话也会给服务器带来一定的压力,因为进程总是在后台运行的.这方面的详细资料可以从网上查到,耀骑网络就不在此多说了,看以下的实现代码:

代码分为4个文件:

文件一:index.php

<?php
$uid=$_GET[‘uid’];
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Comet demo</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<script type=”text/javascript” src=”prototype.js”></script>
</head>
<body><div id=”content”>
</div><p>
<form action=”” method=”get” onsubmit=”comet.doRequest($(‘word’).value);$(‘word’).value=”;return false;”>
<input type=”text” name=”word” id=”word” value=”” />
<input type=”submit” name=”submit” value=”Send” />
</form>
</p>

<script type=”text/javascript”>
var Comet = Class.create();
Comet.prototype = {

timestamp: 0,
url: ‘./backend.php?uid=<?php echo $uid;?>’,
noerror: true,

initialize: function() { },

connect: function()
{
this.ajax = new Ajax.Request(this.url, {
method: ‘get’,
parameters: { ‘timestamp’ : this.timestamp },
onSuccess: function(transport) {
// handle the server response
var response = transport.responseText.evalJSON();
this.comet.timestamp = response[‘timestamp’];
this.comet.handleResponse(response);
this.comet.noerror = true;
},
onComplete: function(transport) {
// send a new ajax request when this request is finished
if (!this.comet.noerror)
// if a connection problem occurs, try to reconnect each 5 seconds
setTimeout(function(){ comet.connect() }, 5000);
else
this.comet.connect();
this.comet.noerror = false;
}
});
this.ajax.comet = this;
},

disconnect: function()
{
},

handleResponse: function(response)
{
$(‘content’).innerHTML += ‘<div>’ + response[‘msg’] + ‘</div>’;
},

doRequest: function(request)
{
new Ajax.Request(this.url, {
method: ‘get’,
parameters: { ‘msg’ : request }
});
}
}
var comet = new Comet();
comet.connect();
//comet.doRequest(<?php echo json_encode($word);?>);
</script>

</body>
</html>

文件二:backend.php

<?phpheader(“Expires: Mon, 26 Jul 1997 05:00:00 GMT”);
header(“Cache-Control: no-cache”);
header(“Pragma: no-cache”);$uid=intval($_GET[‘uid’]);

$filename = dirname(__FILE__).’/’.$uid.’.txt’;

// store new message in the file
/*$msg = isset($_GET[‘msg’]) ? $_GET[‘msg’] : ”;
if ($msg != ”)
{
file_put_contents($filename,$msg);
die();
}*/

// infinite loop until the data file is not modified
$lastmodif = isset($_GET[‘timestamp’]) ? $_GET[‘timestamp’] : 0;
$currentmodif = is_file($filename) ? filemtime($filename) : 0;
while ($currentmodif <= $lastmodif) // check if the data file has been modified
{
usleep(10000); // sleep 10ms to unload the CPU
clearstatcache();
$currentmodif = filemtime($filename);
}

// return a json array
//ob_end_flush();
//ob_implicit_flush(1);
$response = array();
$response[‘msg’] = file_get_contents($filename).’***’.$uid;
$response[‘timestamp’] = $currentmodif;
echo json_encode($response);
flush();

?>

文件三:changeFile.php

<?php
$uid=intval($_GET[‘uid’]);
$data=$_GET[‘data’];
$filename = dirname(__FILE__).’/’.$uid.’.txt’;if($data==file_get_contents($filename)){
die();
}if ($uid>0 && $data != ”)
{
file_put_contents($filename,$data);
die();
}
?>

文件四:prototype.js可以到官网下载,我用的版本是1.5.1.1

测试方法:

1.访问http://localhost/comet/index.php?uid=118

2.访问http://localhost/comet/changFile.php?uid=118$data=hello world

这样你就可以看到效果,这里加一个UID的参数是为了区别不同的用户

发表评论

邮箱地址不会被公开。 必填项已用*标注

+ 86 = 88