【问题标题】:Client progress polling with CPPRestSDK使用 CPPRESTSDK 进行客户端进度轮询
【发布时间】:2017-03-12 15:53:45
【问题描述】:

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

我见过web::http::http_request::set_progress_handler,但我只能看到一种使用方法,如果我设置了一个 websocket 来将进度推送到客户端。但我更愿意使用轮询来监控客户端的进度。一个解决方案is explained here,但我看不出如何用这个库实现它。

【问题讨论】:

    标签: c++ rest asynchronous casablanca


    【解决方案1】:

    首先你需要用一个 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,并提取标头数据。

    【讨论】:

      最近更新 更多