Server sent events work, but with a massive time delay

后端 未结 4 1269
醉话见心
醉话见心 2021-02-15 22:23

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

4条回答
  •  清酒与你
    2021-02-15 22:39

    just add, ob_flush(); before flush() function in stream.php

    updated stream.php script is as below, observe ob_flush() function before flush() function.

    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
        **ob_flush();**
        flush();
    
        // Wait one second.
        sleep(1);
    }
    

提交回复
热议问题