1

I have a task which takes a while to execute, and I would like to launch it and broadcast its progress through a Rest request as described here. I've set up a listener with Client progress polling with CPPRestSDK , but I can't figure out a way of doing this?

I've seen 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 but I can't see how I could implement that with this lib.

Community
  • 1
  • 1
Victor.dMdB
  • 999
  • 2
  • 11
  • 29

1 Answers1

0

First you need to respond with a URL to a progress listener

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);

Then start a thread which allows you to host a separate listener.

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);
  }
}

And then you need to poll that url with your client, and extract the header data.

Victor.dMdB
  • 999
  • 2
  • 11
  • 29