c++ - 使用 CPPRestSDK 进行客户端进度轮询

标签 c++ rest asynchronous casablanca

我有一个任务需要一段时间才能执行,我想启动它并通过 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/

相关文章:

asynchronous - F#异步-两种结构之间的区别

c++ - 我应该根据 CPU 的数量将我的异步调用分成 block 吗?

c++ - 如何修复类中的类型限定符错误?

postgresql - 如何禁用 postgrest 查询的匿名执行

asp.net - 已添加具有相同 key 的项目。 IIS WCF 休息

rest - 使用 RESTful 架构访问多维数据

swift - 使用异步方法的 While 循环

c++ - boost 两个线程

c++ - 使用cmake将两种解决方案合二为一

c# - EF6 ToListAsync 不运行异步但阻塞线程