public static function pushEvent(){ header('Content-Type: text/event-stream'); header('Cache-Control: no-cache'); // 建议不要缓存SSE数据 header('Connection: keep-alive'); //header('X-Accel-Buffering: no');//有可能用nginx的需要设置这个才能把缓冲数据刷出去 $serverTime = time(); $hasData = hFile::cache('pushEvent');//读缓存 if($hasData){ hFile::cache('pushEvent',0);//存缓存 self::sendMsg($serverTime, 'server time: ' . date("h:i:s"),'myevent',100000); }else self::sendMsg('','','',100);//多少毫秒内无数据,再重连 } /** * @param string $id Timestamp/id of this connection. * @param string $msg Line of text that should be transmitted. * @param $event string */ private static function sendMsg($id='', $msg='',$event='',$retry=''){ if(!($id||$event||$msg||$retry)) return; if($retry) echo "retry: $retry" .PHP_EOL; if($id) echo "id: $id" . PHP_EOL; if($event) echo "event: {$event}" . PHP_EOL; if($msg) echo "data: $msg" . PHP_EOL; echo PHP_EOL;//表示本条数据结束 ob_flush(); flush(); }
I wrote it like this (part of the code was copied). The problem is that every time the front end establishes a connection, PHP can only return data once. If you want the next data, rely on this lineself::sendMsg('','','',100);//How many milliseconds There is no data in it, then reconnect
. This line will tell the front end how many milliseconds it will take before re-establishing the connection. This seems to be a waste of resources, and each time you want to output data, you have to read the cache to see if there is a message sent to the user. Way, is there any other good way?
public static function pushEvent(){ header('Content-Type: text/event-stream'); header('Cache-Control: no-cache'); // 建议不要缓存SSE数据 header('Connection: keep-alive'); //header('X-Accel-Buffering: no');//有可能用nginx的需要设置这个才能把缓冲数据刷出去 $serverTime = time(); $hasData = hFile::cache('pushEvent');//读缓存 if($hasData){ hFile::cache('pushEvent',0);//存缓存 self::sendMsg($serverTime, 'server time: ' . date("h:i:s"),'myevent',100000); }else self::sendMsg('','','',100);//多少毫秒内无数据,再重连 } /** * @param string $id Timestamp/id of this connection. * @param string $msg Line of text that should be transmitted. * @param $event string */ private static function sendMsg($id='', $msg='',$event='',$retry=''){ if(!($id||$event||$msg||$retry)) return; if($retry) echo "retry: $retry" .PHP_EOL; if($id) echo "id: $id" . PHP_EOL; if($event) echo "event: {$event}" . PHP_EOL; if($msg) echo "data: $msg" . PHP_EOL; echo PHP_EOL;//表示本条数据结束 ob_flush(); flush(); }
I wrote it like this (part of the code was copied). The problem is that every time the front end establishes a connection, PHP can only return data once. If you want the next data, rely on this lineself::sendMsg('','','',100);//How many milliseconds There is no data in it, then reconnect
. This line will tell the front end how many milliseconds it will take before re-establishing the connection. This seems to be a waste of resources, and each time you want to output data, you have to read the cache to see if there is a message sent to the user. Way, is there any other good way?
Please refer to this article of mine: https://segmentfault.com/a/11...
The most important thing is the following paragraph:
ob_flush(); flush(); //等待3秒钟,开始下一次查询 sleep(3);
After flushing, do not disconnect, but sleep for 3 seconds and then enter the loop again, so that the connection can be maintained and will not be disconnected and reconnected.
Why not do something like this via websocket?