Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[QUESTION] sse流式传输时 前端都断开连接了 后端还在输出 #7293

@ajiader

Description

@ajiader

hyperf怎么判断前端连接已经断开了
使用connection_aborted() 和connection_status() 都不行 连接断开都是返回的0

`
public function streamResponse(Response $swresponse)
{
$response = \Hyperf\Support\make(ResponseInterface::class);

    $eventStream = new \Hyperf\Engine\Http\EventStream($response->getConnection());
   /* foreach ($result as $stream) {
        $eventStream->write($stream['choices'][0]['text']);
    }*/


    //判断客户端是否断开
    while (true) {
        // 模拟动态生成的数据
        $data = [
            'time' => date('Y-m-d H:i:s'),
            'message' => 'Hello, this is an SSE message.'
        ];

        print_r($data);

        // 向客户端发送事件
        $eventStream->write("data: " . json_encode($data) . "\n\n");

        // 休眠1秒,模拟事件流的间隔
        usleep(1000000);  // 1秒
    }
    $eventStream->end();
}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions