【发布时间】:2017-12-15 08:15:12
【问题描述】:
我正在开发涉及大量数据的 php 网站。 我正在尝试实现 comet 而不是简单的 ajax 轮询。
每当有人插入数据时,我都会从数据库中获取数据。我有 2 个 php 页面:
long.php
<script type="text/javascript" >
var lastid = null;
$(document).ready(function(){
lastid = $('.what').children().last().attr('id');
});
var lastid = null;
function waitForMsg(){
lastid = $('.what').children().last().attr('id');
$.ajax({
type:"GET",
url:"pollcheck.php?lastid="+lastid,
async:true,
cache:false,
success: function(data){
$('.what').append(data);
setTimeout('waitForMsg()', 1000);
}
});
}
$(document).ready(function() {
waitForMsg();
});
</script>
和 pollcheck.php :
<?php
include('connection.php');
include('config.php');
$lastmodif = isset($_GET['lastid']) ? $_GET['lastid'] : 0;
$queryfetchpollid = "select * from poll where id > $lastmodif";
$resultfetchpollid = mysqli_query($con, $queryfetchpollid);
$num_rows = mysqli_num_rows($resultfetchpollid);
while($num_rows <= 0) {
usleep(10000);
clearstatcache();
$queryfetchpollid = "select * from poll where id > $lastmodif";
$resultfetchpollid = mysqli_query($con, $queryfetchpollid);
while($resultrow = mysqli_fetch_array($resultfetchpollid)){ $lastmodif = $resultrow[0]; }
$num_rows = mysqli_num_rows($resultfetchpollid);
}
while($resultrow = mysqli_fetch_array($resultfetchpollid)){
$lastmodif = $resultrow[0];
echo "<div class='rowt' id='$resultrow[0]'><div class='no'>$resultrow[0]</div><div class='name'>$resultrow[1]</div></div>";
}
?>
当服务器负载至关重要时,这种方法是否比简单的 ajax 轮询更好? 你知道其他方法吗?
【问题讨论】:
-
还在等吗?