我有一个任务需要一段时间才能执行,我想启动它并通过 Rest 请求广播它的进度 as described here .我已经使用 CPPRestSDK 设置了一个带有客户端进度轮询的监听器,但我想不出这样做的方法?
我看过 web::http::http_request::set_progress_handler但如果我设置一个 websocket 将进度推送给客户端,我只能看到一种使用它的方法。但我更愿意使用轮询来监视客户端的进度。一种解决方案is explained here但我看不出如何用这个库实现它。
最佳答案
首先你需要用一个 URL 响应一个进度监听器
int progress = 0;
std::string progURL = "http://www.example.com/listener";
std::thread progList = std::thread{&ProgressListener, progURL, std::ref(progress)};
web::http::http_response response(web::http::status_codes::Accepted);
response.headers().add("Location", requURL);
request.reply(response);
然后启动一个线程,允许您托管一个单独的监听器。
void ProgressListener( std::string progURL, double &progress ){
web::http::experimental::listener::http_listener progListener(hostURL);
progListener.support(web::http::methods::GET, [&](web::http::http_request request){
web::http::http_response response;
response.set_status_code(web::http::status_codes::OK);
response.headers().add("Progress", progress); // You can call the header anything pretty much
request.reply(response);
}
}
然后您需要与您的客户端轮询该 url,并提取 header 数据。
关于c++ - 使用 CPPRestSDK 进行客户端进度轮询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40327376/