我正在使用Pistache(pistache.2019.02.01.zip,http://pistache.io/),REST C ++框架。
我的可执行文件提供图像,我想创建一个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,它更容易使用。
上周我使用了标头并试图纠正连接HTTP标头中出现故障的默认行为。您可以再次检查并尝试使用先前的提交来实现。记住这个库是pre-alpha版本。你不应该在生产中使用它。