PHP + jQuery - 对数据库数据的长轮询不起作用(它不会打破循环)

标签 php jquery ajax database long-polling

我正在尝试在我的内部网络上实现一个长轮询系统,大多数用户使用 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/

相关文章:

javascript - JS如何在使用ajax后获取嵌入元素的值

javascript - 未处理的拒绝(类型错误): Cannot read property 'map' of undefined when doing React tutorial

jquery - Jsp、Jquery和Json编码字符问题

php - Ajax PHP 变量传递

jquery - 使用 Angular JS 更改元素高度

jQuery - 只执行一次

javascript - 需要在同一个输入框中添加标签和普通文本

php - 本地主机拒绝连接Xampp问题

PHP - 迭代文件并分解 JSON 文本 block

php - 用 Umlaute 替换德语字符到简单的拉丁字符 php