我正在 Laravel 中处理服务器发送事件。我在生产中遇到错误“ob_flush():无法刷新缓冲区。没有缓冲区可以刷新”它在本地主机上工作正常,但在生产中抛出错误
这里是sse代码
public function notificationsCount(Request $request)
{
try {
$response = new \Symfony\Component\HttpFoundation\StreamedResponse(function () use ($request) {
while (true) {
$notif_count = Auth::user()->unreadNotifications->count();
echo json_encode(['data' => $notif_count]) . "\n\n";
ob_flush();
flush();
sleep(1);
if (connection_aborted()) {
break;
}
}
ob_end_flush();
});
$response->headers->set('Content-Type', 'text/event-stream');
$response->headers->set('X-Accel-Buffering', 'no');
$response->headers->set('Cach-Control', 'no-cache');
return $response->send();
} catch (\Exception$e) {
return response()->json(['error' => $e->getMessage()]);
}
}
答案 0 :(得分:0)
如果输出缓冲区处于活动状态(例如通过 ob_flush()
或通过 php.ini 设置),您只需要 ob_start()
试试
ob_start();
echo "foo";
ob_flush();