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 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行