doubaoxue5788 2012-02-21 14:01
浏览 79
已采纳

PHP长轮询失败

I have this loop to long poll:

$time = time(); //send a blank response after 15sec
    while((time() - $time) < 15) {
            $last_modif = filemtime("./logs.txt");
            if($_SESSION['lasttime'] != $last_modif) {
                $_SESSION['lasttime'] = $last_modif;
                $logs = file_get_contents("./logs.txt");
                print nl2br($logs);
                ob_flush();
                flush();
                die();
        }

        usleep(10000);
    }

problem is: the "if" condition is never entered in the middle of the while loop, even if logs.txt is modified. I have to wait 15sec till the next call to this file to obtain the updated content (so it becomes a regular, "setTimeout style" AJAX polling, not a long-poll). Any idea why ?

  • 写回答

1条回答 默认 最新

  • douxia6163 2012-02-21 14:30
    关注

    This is because of the filemtime() function : its results are cached. Thus each time your loop is executed the timestamp's the same for 15 seconds.

    I have not tried it myself, but according to w3cschools :

    The result of this function are cached. Use clearstatcache() to clear the cache.

    Hope that helps !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog