Firstly, sorry for my bad English.
I trying to download file form server by using CURL, but when my app is downloading, it freezes until the download complete.
Here is my code:
HelloWorld::HelloWorld() {
auto button = ui::Button::create("btn.png", "btn.png", "btn.png");
button->ignoreContentAdaptWithSize(false);
button->setAnchorPoint(Vec2(0.5, 0.5));
button->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
button->addClickEventListener(CC_CALLBACK_0(HelloWorld::download, this));
this->addChild(button, 100);
}
int HelloWorld::download()
{
CURL *curl;
CURLcode res;
char *url = "http://www.rarlab.com/rar/winrar-x64-531.exe";
auto root = FileUtils::getInstance()->getWritablePath();
auto filedirectory = root + "file.exe";
curl = curl_easy_init();
if (curl != NULL) {
FILE *fp;
fp = fopen(filedirectory.c_str(), "wb");
if (fp != NULL) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res == CURLE_OK) {
auto label = Label::createWithTTF("Done", "fonts/Marker Felt.ttf", 30);
label->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
this->addChild(label, 100);
}
}
fclose(fp);
}
return 0;
}
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
Please help me, thanks!