Server sent events work, but with a massive time delay

后端 未结 2 1263
心在旅途
心在旅途 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条回答
  •  温柔的废话
    2021-02-15 23:03

    Server.php needs to be as follows:

    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);
    }
    

提交回复
热议问题