【发布时间】:2019-02-25 15:30:33
【问题描述】:
我正在使用 REST C++ 框架 Pistache (pistache.2019.02.01.zip, http://pistache.io/)。
我的可执行文件提供图像,我想创建一个 MJPEG 流。
到目前为止,我的实现如下所示:
void PistMJPEGServer::serveMJPEG(const Rest::Request& request, Http::ResponseWriter response)
{
std::shared_ptr<Http::ResponseWriter> shared_response = std::make_shared<Http::ResponseWriter>(std::move(response));
// the "HandleMJPEG" function is declared as part of my class
handleMJPEG = [shared_response, this](ssize_t bytes)
{
shared_response->headers()
.add<Http::Header::Connection>(Http::ConnectionControl::Close)
.add<Http::Header::CacheControl>(Http::CacheDirective::NoCache)
;
shared_response->send(
Http::Code::Ok,
jpegBoundary
).then(
[shared_response, this](ssize_t bytes)
{
shared_response->headers()
.add<Http::Header::Connection>(Http::ConnectionControl::Close)
.add<Http::Header::CacheControl>(Http::CacheDirective::NoCache)
;
// this method is mine, it returns an image as a buffer
auto buf = this->getImage();
shared_response->send(
Http::Code::Ok,
std::string{buf.begin(), buf.end()},
MIME(Image, Jpeg)
).then(
this->handleMJPEG,
Async::Throw
)
;
},
Async::Throw
);
};
shared_response->headers()
.add<Http::Header::Connection>(Http::ConnectionControl::Close)
.add<Http::Header::CacheControl>(Http::CacheDirective::NoCache)
;
shared_response->send(
Http::Code::Ok,
"",
Http::Mime::MediaType::fromString(
boost::str(boost::format("multipart/x-mixed-replace; boundary=%1%") % jpegBoundary)
)
)
.then(
handleMJPEG,
Async::Throw
)
;
}
递归循环运行良好,但我在浏览器中看不到任何图像。实际上,我没有得到我期望的 Http::ConnectionControl::Close 和 Http::CacheDirective::NoCache ......如果我尝试添加更多标题,我看不到它们或者它们混淆了(如果需要,我可以提供示例)。
我的道路很好,还是我想要实现的目标无法用 Pistache 完成?我以前使用 boost::asio 实现过,但我更愿意使用 Pistache,它更易于使用。
[ 编辑 19-10 :由于它仍然无法正常工作,我将我的实现切换为 boost::asio,它的工作原理就像一个魅力]
【问题讨论】: