|
9 | 9 | #include "base/io-engine.hpp"
|
10 | 10 | #include "base/objectlock.hpp"
|
11 | 11 | #include "base/json.hpp"
|
12 |
| -#include <boost/asio/buffer.hpp> |
13 | 12 | #include <boost/asio/write.hpp>
|
14 | 13 | #include <boost/algorithm/string/replace.hpp>
|
| 14 | +#include <boost/asio/streambuf.hpp> |
15 | 15 | #include <map>
|
16 | 16 | #include <set>
|
17 | 17 |
|
@@ -110,20 +110,16 @@ bool EventsHandler::HandleRequest(
|
110 | 110 | http::async_write(stream, response, yc);
|
111 | 111 | stream.async_flush(yc);
|
112 | 112 |
|
113 |
| - asio::const_buffer newLine ("\n", 1); |
| 113 | + auto adapter(std::make_shared<AsioStreamAdapter<AsioTlsStream>>(stream, yc)); |
| 114 | + JsonEncoder encoder(adapter); |
114 | 115 |
|
115 | 116 | for (;;) {
|
116 |
| - auto event (subscriber.GetInbox()->Shift(yc)); |
117 |
| - |
118 |
| - if (event) { |
119 |
| - String body = JsonEncode(event); |
120 |
| - |
121 |
| - boost::algorithm::replace_all(body, "\n", ""); |
122 |
| - |
123 |
| - asio::const_buffer payload (body.CStr(), body.GetLength()); |
124 |
| - |
125 |
| - asio::async_write(stream, payload, yc); |
126 |
| - asio::async_write(stream, newLine, yc); |
| 117 | + if (auto event(subscriber.GetInbox()->Shift(yc)); event) { |
| 118 | + encoder.Encode(event); |
| 119 | + // Put a newline at the end of each event to render them on a separate line. |
| 120 | + adapter->async_put('\n'); |
| 121 | + // Since shifting the next event may cause the coroutine to yield, we need to flush the |
| 122 | + // stream after each event to ensure that the client receives it immediately. |
127 | 123 | stream.async_flush(yc);
|
128 | 124 | } else if (server.Disconnected()) {
|
129 | 125 | return true;
|
|
0 commit comments