我需要编写一个小型服务来流传输一系列视频图像。我假装使用一个包含多个部分的答案,以便浏览器不断询问下一部分,但是我无法使其正常工作。
void get_webcamsServer( const shared_ptr< Session > session )
{
const auto request = session->get_request( );
const string cameraIdx = request->get_path_parameter( "camera" );
//const string body = string( istreambuf_iterator< char >( stream ), istreambuf_iterator< char >( ) );
const string body = "--frame\r\nContent-Type: image/jpeg\r\n\r\n" + frame[ ncount % 2 ] + "\r\n\r\n";
ncount++;
const multimap< string, string > headers
{
{ "Content-Type", "multipart/x-mixed-replace; boundary=frame" },
{ "Content-Length", ::to_string( body.length( ) ) },
{ "Connection", "keep-alive" }
};
session->yield( OK, body, headers );
this_thread::sleep_for(chrono::milliseconds(250));
}
int main( int argc UNUSED, char **argv UNUSED )
{
// populate the list of frames
ifstream stream0( "frame-0.jpg", ifstream::in );
ifstream stream1( "frame-1.jpg", ifstream::in );
frame.push_back( string( istreambuf_iterator< char >( stream0 ), istreambuf_iterator< char >( ) ) );
frame.push_back( string( istreambuf_iterator< char >( stream1 ), istreambuf_iterator< char >( ) ) );
stream0.close();
stream1.close();
auto webcamsServer = make_shared< Resource >( );
webcamsServer->set_path( "/cameras/{camera: [0-9]*}" );
webcamsServer->set_method_handler( "GET", get_webcamsServer );
auto settings = make_shared< Settings >( );
settings->set_port( 1984 );
settings->set_default_header( "Connection", "close" );
Service service;
service.publish( webcamsServer );
service.start( settings );
return 0;
}
我希望浏览器在连接到http://localhost:1984/cameras/0时(交替地)开始流式传输两个图像...但是只是不起作用..当我使用Firefox时,我什至看不到页面来源(尽管我将主体字符串转储到cout时得到了打印)。
谢谢。