我正在尝试在我的内部网络上实现一个长轮询系统,大多数用户使用 IE,有些也使用移动设备,这就是为什么我尝试使用长轮询而不是使用 websockets 来实现。
我关注了这个视频http://www.screenr.com/SNH我编辑了一些代码来使用我的数据库。 ( Firebird )
一切看起来都不错,但它只是没有打破循环。也许这是一个 child 的错误,但我看不到它,这就是为什么我需要你的帮助!
代码如下:
jQuery + Ajax:
var timestamp = null;
function waitForMsg(){
$.ajax({
type: "GET",
url: "getData.php?timestamp=" + timestamp,
async: true,
cache: false,
success: function(data){
alert('It Works');
var json = eval('(' + data + ')');
timestamp = json['timestamp'];
setTimeout('waitForMsg()',15000);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
alert("A - " + XMLHttpRequest + " - error: " + textStatus + " (" + errorThrown + ")");
setTimeout('waitForMsg()',15000);
}
});
}
$(document).ready(function(){
waitForMsg();
});
</script>
getData.php('DATAHORA'是时间戳字段)
<?php
set_time_limit(0);
@ini_set("memory_limit",'64M');
require_once('../classes/conexao.php');
$banco = Conexao :: getConexao();
$sql = "SELECT FIRST 1 DATAHORA FROM AGENDAMENTOSBBM ORDER BY DATAHORA DESC";
$res = $banco->execute($sql);
$dados = $banco->fetch($res);
if($dados)
$currentmodif = $dados['DATAHORA']);
else
$currentmodif = 0;
$lastmodif = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
while( $currentmodif <= $lastmodif ){
usleep(10000);
$sql = "SELECT FIRST 1 DATAHORA FROM AGENDAMENTOSBBM ORDER BY DATAHORA DESC";
$res = $banco->execute($sql);
$dados = $banco->fetch($res);
if($dados)
$currentmodif = $dados['DATAHORA']);
else
$currentmodif = 0;
}
$response = array();
$response['timestamp'] = $currentmodif;
echo json_encode($response);
?>
当我插入、更新或删除某些数据时,时间戳字段将更新为当前时间戳。 我可以看到页面进入了循环,但我不知道为什么它永远不会结束。
我做错了什么吗?
谢谢
最佳答案
我终于找到了解决方案。
事情就这么简单。我的代码没有关闭与 ibase_close
的连接
我所做的是将其更改为在完成查询过程时关闭。 然后在循环内,我需要再次重新连接服务器。
天哪,我怎么忘了这一点。
谢谢大家。
关于PHP + jQuery - 对数据库数据的长轮询不起作用(它不会打破循环),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18831103/