Refactor EventsHandler to stream responses via HttpResponse::Write()

This commit is contained in:
Johannes Schmidt 2025-07-01 16:30:32 +02:00
parent 937f21d67c
commit d26294a58a

View File

@ -98,33 +98,22 @@ bool EventsHandler::HandleRequest(
EventsSubscriber subscriber (std::move(eventTypes), request.GetLastParameter("filter"), l_ApiQuery); EventsSubscriber subscriber (std::move(eventTypes), request.GetLastParameter("filter"), l_ApiQuery);
server.StartStreaming(); IoBoundWorkSlot dontLockTheIoThread (yc);
response.result(http::status::ok); response.result(http::status::ok);
response.set(http::field::content_type, "application/json"); response.set(http::field::content_type, "application/json");
response.StartStreaming();
response.Flush(yc);
IoBoundWorkSlot dontLockTheIoThread (yc); auto encoder = response.GetJsonEncoder();
http::async_write(stream, response, yc);
stream.async_flush(yc);
asio::const_buffer newLine ("\n", 1);
for (;;) { for (;;) {
auto event (subscriber.GetInbox()->Shift(yc)); auto event(subscriber.GetInbox()->Shift(yc));
if (event) { if (event) {
String body = JsonEncode(event); encoder.Encode(event);
response.body() << '\n';
boost::algorithm::replace_all(body, "\n", ""); response.Flush(yc);
asio::const_buffer payload (body.CStr(), body.GetLength());
asio::async_write(stream, payload, yc);
asio::async_write(stream, newLine, yc);
stream.async_flush(yc);
} else if (server.Disconnected()) {
return true;
} }
} }
} }