HttpClient/Downloader cancel/abort a request

HttpClient/Downloader cancel/abort a request
0

#4

Here is my class using curl to download files:
https://pastebin.com/vyiV0wPP .h file
https://pastebin.com/UNWHBGJa .cpp file

you can set public variable downloading to false for canceling download.


Download a list of files from a url?
#5

The question for the millon dollars is:

HttpClient provides a solution for it? :sunglasses:


#6

Okay, your checking in every progress update callback, if the download was canceled. The same behavior could maybe manually be implemented into the Downloader (onTaskProgress ??) from cocos, but I don’t unterstand, why this isn’t by default?

@tranthor I believe currently will no one win the million dollar.


#7

Hi @mars3142
If you want to abort the request then We are handling this via below code into destructor, you just need to save pointer to HttpRequest and set nullptr to responseCallback and call release.
We have multiple request so we store into vec.

 LoginMode::~LoginMode() {
	for (int i = 0;i < (int)httpVec.size();i++)
	{
		httpVec.at(i)->setResponseCallback(nullptr);
		httpVec.at(i)->release();
	}
}

#8

@smitpatel88 i’m trying to test your alternative.
How do you fill httpVec ? I’m trying like this but i’ve problems:

Declaration in my singleton class:
vector<cocos2d::network::HttpRequest*> httpVec;

In class method:

network::HttpRequest* request = new network::HttpRequest();
request->setUrl(urlRequest);
request->setRequestType(network::HttpRequest::Type::GET);
request->setResponseCallback([](network::HttpClient* client, network::HttpResponse* response) {

 //Content

});
network::HttpClient::getInstance()->send(request);
request->release();

httpVec.push_back(request);

The error occurs in push_back line. Probably because of bad handle pointers.


#9

As far, as I understand the code from @smitpatel88 you didn’t call release directly after creation, but within the loop of the destructor. But the code didn’t cancel long running downloads, it just remove the callback, so the response will never be processed. Right.


#10

Yes, I understand like you, I never “cancel” the request but if I set nullptr is equal to cancel it. So, it could solve the “cancel” problem. But I wonder how can I fill httpVec… hmm…


#11

#12

@CAPONE sorry, I don’t understand your example.
How can I fill httpVec, following the example?

// cc @smitpatel88


#13

As i told that we have multiple request so we have to use vec, but if you have single request then use this,

 LoginMode::~LoginMode() {
	httpReq->setResponseCallback(nullptr);
	httpReq->release();
}

#14

@smitpatel88 i’ve multiple request, how do you fill the vec? look my last post, i’ve an error when I try to insert in the vec using push_back


#15

Are you taking this pointer as local variable? It must be global.
Declare in header file.


#16

Thanks @smitpatel88 , that was the problem.
However, this alternative didn’t solve my problem.

My program crashes inside the for, at this line:
httpVec.at(i)->setResponseCallback(nullptr);

Sometimes in the first element, sometimes in the second, etc. but it crashes.
Maybe because if the http response is in “action” i can’t set null there.

i’m requesting http info when I touch a button.
but when i touch the “Close” button i would like to cancel all requests. Because of this, i put this code in “close” button action:

for (int i = 0;i < (int)httpVec.size();i++)
{
	httpVec.at(i)->setResponseCallback(nullptr);
	httpVec.at(i)->release();
}

but it crashes when i touch it, as i said before.


#17

Try giving callback method.
request->setResponseCallback(CC_CALLBACK_2(LoginMode::onHttpRequestCompleted, this));


#18

Nope. I continue with the problem…


#19

can you pls share me simple helloworld files to reproduce this?


#20

Yep.
I uploaded here an example.
As you can see, you’ve a green and red button.

If you touch green button, i’m trying to request 1000 cocos2d page (GET’s).
If you touch red button, i’m setting to NULL all elements in the vector.

If you touch green button and quicky you touch red button. The program crashes.


#21

@smitpatel88 did you test it? if you can’t compile the project you could create a new project and copy classes folder.


#22

Hey buddy, i am really sry.
I am very busy these days with 2 project.
But i will definitely check within 2-3 days and will revert you back.


#23

this examples have some error, here updated code
https://pastebin.com/GdUmNcMh .h file
https://pastebin.com/jfC2tZhN .cpp file