使用 CPPRestSDK 进行客户端进度轮询

Client progress polling with CPPRestSDK

我有一个任务需要一段时间才能执行,我想启动它并通过 Rest 请求广播它的进度 as described here。我已经使用 CPPRestSDK 设置了客户端进度轮询的侦听器,但我想不出这样做的方法?

我看过 web::http::http_request::set_progress_handler but I can only see a way to use that if I set up a websocket to push the progress to the client. But I would prefer to monitor progress from the client using polling. A solution 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 数据。