doutang7661 2013-11-24 14:51
浏览 73

长轮询问题

I have strange issue, I'm doing long polling to check if something new appeared in MySQL table, everything works fine, I'm getting messages as soon as they appear in table. but problem is when I'm trying to reload my page (F5) I have to wait until server side function is completed and then my page refreshes.

For example my long polling start and I refresh page in first second of execution, I have to wait 29 seconds for page to refresh.

Does anyone knows whats going on?

Thanks in advance!

here is code

php

public function longPolling() {
    if (isset($_POST['timestamp'])) {
        $timestamp = $_POST['timestamp'];
    }
    $end = time() + 20;
    while (time() <= $end) {
        $data = $this->db->select("SELECT id, status, error, start, end, ackby FROM event WHERE start > '$timestamp'", 0);
        if ($data) {
            echo json_encode(array('status' => 1, 'timestamp' => date('Y-m-d H:i:s')));
            break;
        } 
        usleep(25000);
    }
}

JS

function startPoll(timestamp) {
    $.ajax({
        url: 'dashboard/longPolling',
        type: 'POST',
        dataType: 'json',
        timeout: 30000,
        cache: false,
        data: {
            timestamp: timestamp
        },
        success: function(data) {
            if (data != null && data.status == 1) {
                oTable.fnReloadAjax();
                timestamp = data.timestamp;
            }
            setTimeout(function(){
                startPoll(timestamp);
            }, 100)
        }

    });
}

P.S. ignore_user_abort is disabled.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图