-1
我目前正在开发使用oxygine/SDL/coco2d手机游戏。 但正常的http请求似乎阻止渲染更新(挂起)获得响应,我已经尝试了很多C++请求库,但无法找到。 是否有任何http客户端可以应用于移动http请求而不会阻塞UI线程。我如何能够让C++的http请求,而不会阻塞UI线程
我目前正在开发使用oxygine/SDL/coco2d手机游戏。 但正常的http请求似乎阻止渲染更新(挂起)获得响应,我已经尝试了很多C++请求库,但无法找到。 是否有任何http客户端可以应用于移动http请求而不会阻塞UI线程。我如何能够让C++的http请求,而不会阻塞UI线程
你可以使用backcurl >>https://github.com/Taymindis/backcurl,这仅仅只是依赖于基于libcurl的。您可以通过使用bcl::setOpts(....)
来随意自定义请求,它适用于非阻止UI请求。
示例代码
// Derived from example/main.cpp
void doGuiWork() {
printf("\r %s --- %d", "Drawing thousand Pieces of Color with count elapsed ", countUI++);
}
void doUpdate() {
bcl::LoopBackFire();
}
void doRunOnUI() {
bool gui_running = true;
std::cout << "Game is running thread: ";
bcl::executeOnUI<std::string>([](bcl::Request * req) -> void {
bcl::setOpts(req, CURLOPT_URL , "http://www.google.com",
CURLOPT_FOLLOWLOCATION, 1L,
CURLOPT_WRITEFUNCTION, &bcl::writeContentCallback,
CURLOPT_WRITEDATA, req->dataPtr,
CURLOPT_USERAGENT, "libcurl-agent/1.0",
CURLOPT_RANGE, "0-200000"
);
}, [&](bcl::Response * resp) {
printf("On UI === %s\n", resp->getBody<std::string>()->c_str());
printf("Done , stop gui running with count ui %d\n", countUI);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
gui_running = false;
});
while (gui_running) {
doGuiWork();
doUpdate();
std::this_thread::sleep_for(std::chrono::milliseconds(1000/16));
}
}
谢谢,真的是纯libcurl的,我喜欢它:) – Oktaheta