mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-27 07:34:15 +02:00
Refactor EventsHandler to stream responses via HttpResponse::Write()
This commit is contained in:
parent
19b392c3c4
commit
5ff416f725
@ -98,32 +98,23 @@ 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();
|
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
IoBoundWorkSlot dontLockTheIoThread (yc);
|
IoBoundWorkSlot dontLockTheIoThread (yc);
|
||||||
|
|
||||||
http::async_write(stream, response, yc);
|
auto writer = std::make_shared<BeastHttpMessageAdapter<HttpResponse>>(response);
|
||||||
stream.async_flush(yc);
|
JsonEncoder encoder(writer);
|
||||||
|
|
||||||
asio::const_buffer newLine ("\n", 1);
|
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
auto event (subscriber.GetInbox()->Shift(yc));
|
auto event(subscriber.GetInbox()->Shift(yc));
|
||||||
|
|
||||||
if (event) {
|
if (event && response.IsWritable()) {
|
||||||
String body = JsonEncode(event);
|
encoder.Encode(event);
|
||||||
|
response.body() << '\n';
|
||||||
boost::algorithm::replace_all(body, "\n", "");
|
response.Write(yc);
|
||||||
|
} else {
|
||||||
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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user