drogon/examples/async_stream/main.cc
tripleslash 6b36b3a4f9
Support asynchronous sending of chunked responses (trantors new API) (#1886)
Co-authored-by: fantasy-peak <1356346239@qq.com>
Co-authored-by: Michael Bleis <michael.bleis@aracom.de>
2024-01-15 23:26:27 +08:00

34 lines
1.3 KiB
C++

#include <drogon/drogon.h>
#include <chrono>
#include <memory>
using namespace drogon;
using namespace std::chrono_literals;
int main()
{
app().registerHandler(
"/stream",
[](const HttpRequestPtr &,
std::function<void(const HttpResponsePtr &)> &&callback) {
auto resp = drogon::HttpResponse::newAsyncStreamResponse(
[](drogon::ResponseStreamPtr stream) {
std::thread([stream =
std::shared_ptr<drogon::ResponseStream>{
std::move(stream)}]() mutable {
std::cout << std::boolalpha << stream->send("hello ")
<< std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << std::boolalpha << stream->send("world");
std::this_thread::sleep_for(std::chrono::seconds(2));
stream->close();
}).detach();
});
resp->setContentTypeCodeAndCustomString(
ContentType::CT_APPLICATION_JSON, "application/json");
callback(resp);
});
LOG_INFO << "Server running on 127.0.0.1:8848";
app().addListener("127.0.0.1", 8848).run();
}