给cocos2d-x2.02的lua加异步httpclient

ocos2d-x 2.02 多了一个HttpClient的扩展包, 是异步请求的. 但是却没有映射到lua中.
所以我稍微改了改, 写了个MyHttpClient, 方便lua的使用

lua里使用:

1
2
3
4
5
    function hello(code,data)
        print(code..","..data);
    end
    MyHttpClient:doGet("http://www.sina.com.cn/",hello);
    

MyHttpClient.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
        #ifndef __MyHttpClient_H_
        #define __MyHttpClient_H_

        #include "cocos2d.h"
        #include "HttpClient.h"

        using namespace cocos2d;
        using namespace extension;

        class MyHttpClient : public cocos2d::CCObject
        {
        public:
            static void doGet(const char* url,int handler);
            virtual void onHttpRequestCompleted(cocos2d::CCNode *sender, void *data);
            virtual void executeFunction(int responseCode, const char* resp);

        private:
            int m_nHandler;
        };

        #endif //__MyHttpClient_H_
    

MyHttpClient.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
        //
        // Created by liangwei on 12-9-10.
        //
        // To change the template use AppCode | Preferences | File Templates.
        //
        #include "MyHttpClient.h"
        #include "CCLuaEngine.h"

        void MyHttpClient::doGet(const char* url,int handler)
        {
            MyHttpClient *myHttpClient=new MyHttpClient();
            CCHttpRequest* request = new CCHttpRequest();
            request->setUrl(url);
            myHttpClient->m_nHandler=handler;
            request->setRequestType(CCHttpRequest::kHttpGet);
            request->setResponseCallback(myHttpClient, callfuncND_selector(MyHttpClient::onHttpRequestCompleted));
            CCHttpClient::getInstance()->send(request);
            request->release();
        }

        void MyHttpClient::executeFunction(int responseCode, const char* data)
        {
            CCScriptEngineProtocol *engine = CCScriptEngineManager::sharedManager()->getScriptEngine();
            lua_State* m_state=engine->getLuaState();
            lua_pushinteger(m_state, responseCode);
            lua_pushstring(m_state,data);
            engine->executeFunctionByHandler(this->m_nHandler, 2);
        }

        void MyHttpClient::onHttpRequestCompleted(CCNode *sender, void *resp) {
            CCLog("onHttpRequestCompleted");

            CCHttpResponse *response = (CCHttpResponse*)resp;

            if (!response)
            {
                return;
            }

            int statusCode = response->getResponseCode();
            std::vector *buffer = response->getResponseData();
            char data[buffer->size()+1];
            for (unsigned int i = 0; i < buffer->size(); i++)
            {
                data[i]=(*buffer)[i];
            }
            data[buffer->size()]='\0';

            this->executeFunction(statusCode, data);
        }
    

liangwei_extension.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        #ifndef __LIANGWEI_EXTENSION_H_
        #define __LIANGWEI_EXTENSION_H_

        extern "C" {
        #include "tolua++.h"
        #include "tolua_fix.h"
        }
        #include "cocos2d.h"

        using namespace cocos2d;

        TOLUA_API int tolua_liangwei_extension_open(lua_State* tolua_S);

        #endif // __COCOS2DX_EXTENSION_NETWORK_H_
    

liangwei_extension.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
        #include "liangwei_extension.h"
        #include "MyHttpClient.h"

        static int tolua_liangwei_MyHttpClient_doGet00(lua_State* tolua_S)
        {
            tolua_Error tolua_err;
            if (
                    !tolua_isusertable(tolua_S,1,"MyHttpClient",0,&amp;tolua_err) ||
                            !tolua_isstring(tolua_S,2,0,&amp;tolua_err) ||
                            !tolua_isfunction(tolua_S,3,&amp;tolua_err) ||
                            !tolua_isnoobj(tolua_S,4,&amp;tolua_err)
                    )
                goto tolua_lerror;
            else
            {
                const char* url = ((const char*)  tolua_tostring(tolua_S,2,0));
                int funcID = (tolua_ref_function(tolua_S,3,0));
                {
                    MyHttpClient::doGet(url, funcID);
                }
            }
            return 1;
            tolua_lerror:
                    tolua_error(tolua_S,"#ferror in function 'node'.",&amp;tolua_err);
            return 0;

        }

        TOLUA_API int tolua_liangwei_extension_open (lua_State* tolua_S)
        {
            tolua_open(tolua_S);

            tolua_usertype(tolua_S,"MyHttpClient");

            tolua_module(tolua_S,NULL,0);
            tolua_beginmodule(tolua_S,NULL);

            //注册函数和类
            tolua_cclass(tolua_S, "MyHttpClient", "MyHttpClient", "CCObject", NULL);
            tolua_beginmodule(tolua_S,"MyHttpClient");
            tolua_function(tolua_S,"doGet",tolua_liangwei_MyHttpClient_doGet00);
            tolua_endmodule(tolua_S);

            tolua_endmodule(tolua_S);
            return 1;
        }

        #if defined(LUA_VERSION_NUM) &amp;&amp; LUA_VERSION_NUM &gt;= 501
        TOLUA_API int luaopen_liangwei_extension (lua_State* tolua_S) {
            return tolua_liangwei_extension_open(tolua_S);
        };
        #endif
    

lua的绑定解析可以参考:tolua映射解析

cocos2d-x 的HttpClient扩展需要curl模块
编译参考:curl for ios 的编译方法及脚本