<?php namespace App\Service\Server; use Hyperf\Dispatcher\HttpDispatcher; use Hyperf\ExceptionHandler\ExceptionHandlerDispatcher; use Hyperf\HttpServer\ResponseEmitter; use Hyperf\HttpServer\Server; use Psr\Container\ContainerInterface; use Swoole\Http\Request; use Swoole\Http\Response; class StreamServer extends Server { const STREAM_URL = [ '/chat/stream' ]; public function __construct(ContainerInterface $container, HttpDispatcher $dispatcher, ExceptionHandlerDispatcher $exceptionHandlerDispatcher, ResponseEmitter $responseEmitter) { parent::__construct($container, $dispatcher, $exceptionHandlerDispatcher, $responseEmitter); } /** * @param Request $request * @param Response $response */ public function onRequest($request, $response): void { $pathInfo = $request->server['path_info']; if (in_array($pathInfo, self::STREAM_URL)) { $response->header('Content-Type', 'text/event-stream'); $response->header('Access-Control-Allow-Origin', '*'); $response->header('Access-Control-Allow-Methods', 'GET'); $response->header('Cache-Control', 'no-cache'); $response->header('Connection', 'keep-alive'); } parent::onRequest($request, $response); } }