我是新來的彗星和做一個簡單的應用程序。 我的HTML文件看起來像彗星 - PHP錯誤顯示
<!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>testing comet</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jquery-1.9.0.js"></script>
</head>
<body>
<div id="content">
</div>
<p>
<form action="" method="get" onsubmit="comet.doRequest($('#word').val());$('#word').val('');return false;">
<input type="text" name="word" id="word" value="" />
<input type="submit" name="submit" value="Send" />
</form>
</p>
<script type="text/javascript">
// comet implementation
var Comet = function (data_url) {
this.timestamp = 0;
this.url = data_url;
this.noerror = true;
//alert(data_url);
this.connect = function() {
var self = this;
$.ajax({
type : 'get',
url : this.url,
dataType : 'json',
data : {'timestamp' : self.timestamp},
success : function(response) {
self.timestamp = response.timestamp;
self.handleResponse(response);
self.noerror = true;
},
complete : function(response) {
// send a new ajax request when this request is finished
if (!self.noerror) {
alert("error");
// if a connection problem occurs, try to reconnect each 5 seconds
setTimeout(function(){ comet.connect(); }, 5000);
}else {
// persistent connection
self.connect();
}
self.noerror = false;
}
});
}
this.disconnect = function() {}
this.handleResponse = function(response) {
$('#content').append('<div>' + response.msg + '</div>');
}
this.doRequest = function(request) {
$.ajax({
type : 'get',
url : this.url,
data : {'msg' : request}
});
}
}
var comet = new Comet('./backend.php');
comet.connect();
</script>
</body>
</html>
backend.php看起來像
<?php
$dr=DIRECTORY_SEPARATOR;
$filename = dirname(__FILE__).$dr.'data.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 = filemtime($filename);
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
$response = array();
$response['msg'] = file_get_contents($filename);
$response['timestamp'] = $currentmodif;
echo json_encode($response);
flush();
data.txt中包含一個空白文件。現在
我的問題是
if (!self.noerror) {
alert("error");
這將執行和顯示時刻警惕「錯誤」。但是,如果我評論那個警報部分,它會很好地工作。有什麼問題嗎 ?
當我使用螢火蟲跟蹤進程時,根據這些請求獲取像這樣的致命錯誤。
任何一個請幫我 在此先感謝
感謝您的評論..更多的疑問..是這種類型的彗星 - PHP的設置是可靠的共享服務器?說是否有更多的用戶請求同一時間? – ramesh
服務器可以同時處理很多請求 - 我不會三思而後行。 –