Server sent events work, but with a massive time delay

后端 未结 2 1265
心在旅途
心在旅途 2021-02-15 22:22

Ill start off by saying this works perfectly on my local machine, the js example below connects to stream.php and receives a continuous update of the servers current time every

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-15 23:04

    @Derrick, your suggested ob_end_flush(); line got me close, but in more complex PHP than hello world code, I was still getting unwanted reopens on my SSE connections (I still don't fully understand why ob_end_flush() was doing that to me). So here's the pattern I'm now using (otherwise identical to your stream.php). In English, I'm turning off PHP output buffering before entering my infinite loop:

    // per http://www.php.net/manual/en/book.outcontrol.php:
    //     Clean (erase) the output buffer and turn off output buffering
    ob_end_clean();
    
    // how long PHP script stays running/SSE connection stays open (seconds)
    set_time_limit(60);
    while (true) {
    
      // use @Derrick's header/send code here
    
      ob_flush();  // note I don't turn off output buffering here again
      flush();
      sleep(1);
    }
    

提交回复
热议问题