How to use asynchronousHTTP request in cocos2d-x

How to use asynchronousHTTP request in cocos2d-x
0
#1

Hi,
I try to create a way to use http request on my game,
I do it using java on android and obj-c on ios,
but I want to use c++ to create one single code for both plataforms.
I create a synchronous http request using this post
[[http://www.jesusbosch.com/2012/08/internet-communications-with-cocos2d-x.html]]

But now I want to create a asynchronous http request, because a month ago I got one of my games repproved by samsungapps because I use a synchronous http request(the server take long time response and the system show an ANR[app not respond] error)

The libcurl have the multi interface, but I look in the official website of libcurl and can’t create a example using it.
Anyone could help create a asynchronous http request, using libcurl or using another approach?

0 Likes

#2

Using curl you can do this

CURLM* multi_handle=NULL;
   CURL *curl;
CURLMcode res;

    curl = curl_easy_init();
    if (curl)
    {

        //your curl setopt

        multi_handle = ::curl_multi_init();
        res = curl_multi_add_handle(multi_handle, curl);

        int still_running=0;
        do {
            while(::curl_multi_perform(multi_handle, &still_running) ==
                  CURLM_CALL_MULTI_PERFORM);
        } while (still_running);

        curl_easy_cleanup(curl);
        if (res == 0)
        {
                  //if ok
               }
     }

hope it helps :slight_smile:

0 Likes

#3

Fabio Cunha wrote:

Hi,
I try to create a way to use http request on my game,
I do it using java on android and obj-c on ios,
but I want to use c++ to create one single code for both plataforms.
I create a synchronous http request using this post
[[http://www.jesusbosch.com/2012/08/internet-communications-with-cocos2d-x.html]]
>
But now I want to create a asynchronous http request, because a month ago I got one of my games repproved by samsungapps because I use a synchronous http request(the server take long time response and the system show an ANR[app not respond] error)
>
The libcurl have the multi interface, but I look in the official website of libcurl and can’t create a example using it.
Anyone could help create a asynchronous http request, using libcurl or using another approach?

hey, you can use CCHttpClient.cpp in version 2.0.2 which is at https://github.com/cocos2d/cocos2d-x/tree/gles20/extensions/network

0 Likes

#4

The example of libcurl using multi works perfectly, I’ll make more tests to see if works the way I need.
Thanks ‘Seppe R’.

About the CCHttpClient, how I can use it? It’s good to have more options to use.

0 Likes

#5

Fabio Cunha wrote:

The example of libcurl using multi works perfectly, I’ll make more tests to see if works the way I need.
Thanks ‘Seppe R’.
>
About the CCHttpClient, how I can use it? It’s good to have more options to use.

https://github.com/cocos2d/cocos2d-x/tree/gles20/samples/TestCpp/Classes/ExtensionsTest/NetworkTest

0 Likes

#6

Thanks fot the tip ‘C Zhang’.

0 Likes

#7

This is not a solution for the problem rather a query. I have a module in C++ and I have created 10 threads to send different curl requests for a telecom product. Now to distribute the load and handle asynchronous requests concurrently I want to use curl_multi. But I am not sure how to fetch requests and send them to curl asynchronously. Is there anyway it can be done?

0 Likes