C++ pistache 和 MJPEG 服务器
Posted
技术标签:
【中文标题】C++ pistache 和 MJPEG 服务器【英文标题】:C++ pistache and MJPEG server 【发布时间】: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::stringbuf.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,它的工作原理就像一个魅力]
【问题讨论】:
【参考方案1】:上周我使用了标头,并尝试更正连接 HTTP 标头中的默认行为,这是错误的。您可以再次检查并尝试使用较早的提交来实现。记住这个库是 pre-alpha 版本。你不应该在生产中使用它。
【讨论】:
它不会改变任何东西。我今天得到了 pistache-master 并用它编译。使用上面的代码,我只得到“multipart/x-mixed-replace;boundary=--jpegboundary”标题。我没有得到 Http::ConnectionControl::Close 和 Http::CacheDirective::NoCache。 “连接:关闭”行为会在发送第一个标头后关闭连接。 连接已关闭,就像我们有这样的事情:send().then( CloseConnection, CloseConnection ).then( yourResolveHandler, yourRejectHandler ) 尝试使用keep-alive而不是close。以上是关于C++ pistache 和 MJPEG 服务器的主要内容,如果未能解决你的问题,请参考以下文章