php – 服务器发送事件工作,但有很长的时间延迟

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 服务器发送事件工作,但有很长的时间延迟脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
开始说这在我的本地机器上完美运行,下面的js示例连接到stream.PHP并且每秒接收服务器当前时间的连续更新.

的index.PHP

VAR source = new EventSource("stream.PHP");

source.addEventListener('message',function(e) {
    console.LOG(e);
},false);

source.addEventListener('oPEn',false);

source.addEventListener('error',function(e) {
    if (e.readystate == EventSource.CLOSED) {
        console.log('closed');
    }
},false);

stream.PHP

while(true)
{
    // Headers must be PRocessed line by line.
    header('Content-type: text/event-stream');
    header('Cache-Control: no-cache');

    // Set data line
    print "data: " . date( 'G:H:s',time() ) . PHP_EOL . PHP_EOL;

    // Toilet
    flush();

    // Wait one second.
    sleep(1);
}

上传到live dev之后我确实预计会有一点延迟.服务器.但是有大约15到20分钟的时间延迟.在我看到第一个条目之前.

连接不会丢失. (问题.现在已经过了40分钟.)这只是一个apache循环问题(意味着是时候看看网络套接字了)还是我可以做些什么来解决这个问题?

Server.PHP需要如下:

stream.PHP

while(true)
{
    // Headers must be processed line by line.
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');

    // Set data line
    print "Event: server-time" . PHP_EOL;
    print "data: " . date( 'G:H:s',time() ) . PHP_EOL;
    print PHP_EOL;

    ob_end_flush();     // Strange behavIoUr,will not work
    flush();            // Unless both are called !

    // WaIT one second.
    sleep(1);
}

脚本宝典总结

以上是脚本宝典为你收集整理的php – 服务器发送事件工作,但有很长的时间延迟全部内容,希望文章能够帮你解决php – 服务器发送事件工作,但有很长的时间延迟所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。