2016-10-30 31 views
1

我有一項任務需要一段時間才能執行,我想啓動它並通過休息請求as described here廣播其進度。我已經用CPPRestSDK建立了一個客戶端進程輪詢監聽器,但我無法弄清楚這樣做的方式?使用CPPRestSDK進行客戶端進度輪詢

我見過web::http::http_request::set_progress_handler,但我只能看到一個方式來使用,如果我成立了一個WebSocket的推進步到客戶端。但我更願意使用輪詢來監視客戶端的進度。一個解決方案is explained here,但我不明白我可以如何實現這個lib。

回答

0

首先你需要用一個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,並提取標題數據。