集成cocos2dx中的CHttpClient到lua中

本文详细介绍了如何在 Cocos2dx 中自定义 HttpClient 类,通过封装 CCHttpRequest 类并实现 lua 回调功能,实现 HTTP 请求的灵活控制与数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不知道为什么cocos2dx中似乎没有把extensions/network里的HttpClient集成进去,自己试了一下DIY也不是很难,现总结如下

首先CCHttpRequest中的setResponseCallback需要处理一下,因为这里我们需要的是一个lua的回调而并非c++的,

处理方案参考CCSchduler中的scheduleScriptFunc,采用lua的handler来解决

最终代码如下:

pkg文件

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class CCHttpClient : CCObject  
  2. {  
  3.     static CCHttpClient *getInstance();  
  4.       
  5.     static void destroyInstance();  
  6.           
  7.     void send(CCHttpRequest* request);  
  8.     
  9.     void setTimeoutForConnect(int value);  
  10.       
  11.     int getTimeoutForConnect();  
  12.       
  13.     void setTimeoutForRead(int value);  
  14.       
  15.     int getTimeoutForRead();  
  16. };  
  17.   
  18.   
  19. class CCHttpRequest : CCObject  
  20. {  
  21.     typedef enum  
  22.     {  
  23.         kHttpGet,  
  24.         kHttpPost,  
  25.         kHttpPut,  
  26.         kHttpDelete,  
  27.         kHttpUnkown,  
  28.     } HttpRequestType;  
  29.       
  30.       
  31.     void setRequestType(HttpRequestType type);  
  32.   
  33.   
  34.     HttpRequestType getRequestType();  
  35.       
  36.     void setUrl(const char* url);  
  37.   
  38.   
  39.     const char* getUrl();  
  40.       
  41.     void setRequestData(const char* buffer, unsigned int len);  
  42.   
  43.   
  44.     char* getRequestData();  
  45.   
  46.   
  47.     int getRequestDataSize();  
  48.       
  49.     void setTag(const char* tag);  
  50.   
  51.   
  52.     const char* getTag();  
  53. };  
  54.   
  55.   
  56. class CCLuaHttpRequest: CCHttpRequest  
  57. {  
  58. <span style="white-space:pre">  </span>void setResponseScriptCallback(LUA_FUNCTION aHandler);  
  59.   
  60.   
  61. <span style="white-space:pre">  </span>static CCLuaHttpRequest* create();  
  62.   
  63.   
  64. <span style="white-space:pre">  </span>static bool mkdirs(std::string aDir);  
  65. };  

LuaHttpRequest.h

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #ifndef CCLUAHTTPREQUEST_H_  
  2. #define CCLUAHTTPREQUEST_H_  
  3.   
  4. #include <network/HttpClient.h>  
  5.   
  6. USING_NS_CC;  
  7. USING_NS_CC_EXT;  
  8.   
  9. class CCLuaHttpRequest: public CCHttpRequest  
  10. {  
  11.     public:  
  12.   
  13.         CCLuaHttpRequest();  
  14.   
  15.         virtual ~CCLuaHttpRequest();  
  16.   
  17.     public:  
  18.   
  19.         static CCLuaHttpRequest* create();  
  20.   
  21.         static bool mkdirs(std::string aDir);  
  22.   
  23.         /** 
  24.          * 设置一个用于回调的lua函数 
  25.          */  
  26.         void setResponseScriptCallback(unsigned int aHandler);  
  27.   
  28.     private:  
  29.   
  30.         /** 
  31.          * 默认的用于c++的回调,由这里统一处理到lua的回调 
  32.          */  
  33.         void responseScriptCallback(CCHttpClient* apClient, CCHttpResponse* apResponse);  
  34.   
  35.     private:  
  36.   
  37.         /** 
  38.          * 当前保存的handler 
  39.          */  
  40.         unsigned int mHandler;  
  41. };  
  42.   
  43. #endif /* CCLUAHTTPREQUEST_H_ */  

LuaHttpRequest.cpp

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "LuaHttpRequest.h"  
  2. #include "CCLuaEngine.h"  
  3. #include <sys/stat.h>  
  4. #include <sys/types.h>  
  5. #include <errno.h>  
  6.   
  7. CCLuaHttpRequest::CCLuaHttpRequest() :  
  8.         mHandler(0)  
  9. {  
  10.       
  11. }  
  12.   
  13. CCLuaHttpRequest::~CCLuaHttpRequest()  
  14. {  
  15. }  
  16.   
  17. CCLuaHttpRequest* CCLuaHttpRequest::create()  
  18. {  
  19.     return new CCLuaHttpRequest;  
  20. }  
  21.   
  22. bool CCLuaHttpRequest::mkdirs(std::string aDir)  
  23. {  
  24.     if (aDir.size() == 0)  
  25.     {  
  26.         return true;  
  27.     }  
  28.   
  29.     CCLOG("creating dir:%s", aDir.c_str());  
  30.     bool isFirst = aDir[0] == '/';  
  31.     bool isLast = false;  
  32.     for (unsigned int i = 1; i < aDir.size(); i++)  
  33.     {  
  34.         if (aDir[i] == '/')  
  35.         {  
  36.             if (isLast)  
  37.             {  
  38.                 continue;  
  39.             }  
  40.             isLast = true;  
  41.   
  42.             if (isFirst)  
  43.             {  
  44.                 isFirst = false;  
  45.                 continue;  
  46.             }  
  47.   
  48.             std::string path = aDir.substr(0, i);  
  49.             if (CCFileUtils::sharedFileUtils()->isFileExist(path.c_str()))  
  50.             {  
  51.                 CCLOG("path:%s exist", path.c_str());  
  52.                 continue;  
  53.             }  
  54.             int ret = mkdir(path.c_str(), 0700);  
  55.             if (ret && errno != EEXIST)  
  56.             {  
  57.                 CCLOG("mkdir:%s failed", path.c_str());  
  58.                 return false;  
  59.             }  
  60.             else  
  61.             {  
  62.                 CCLOG("path:%s created", path.c_str());  
  63.             }  
  64.         }  
  65.         else  
  66.         {  
  67.             isLast = false;  
  68.         }  
  69.     }  
  70.     return true;  
  71. }  
  72.   
  73. void CCLuaHttpRequest::setResponseScriptCallback(unsigned int aHandler)  
  74. {  
  75.     mHandler = aHandler;  
  76.     setResponseCallback(this, httpresponse_selector(CCLuaHttpRequest::responseScriptCallback));  
  77. }  
  78.   
  79. void CCLuaHttpRequest::responseScriptCallback(CCHttpClient* apClient, CCHttpResponse* apResponse)  
  80. {  
  81.     CCLuaEngine* pEngine =  
  82.             dynamic_cast<CCLuaEngine*>(CCScriptEngineManager::sharedManager()->getScriptEngine());  
  83.     CCLuaStack* pStack = pEngine->getLuaStack();  
  84.     bool isSucceed = apResponse->isSucceed();  
  85.     int status = apResponse->getResponseCode();  
  86.     const char* errorBuffer = apResponse->getErrorBuffer();  
  87.     std::vector<char>* headerBuffer = apResponse->getResponseHeader();  
  88.     std::vector<char>* bodyBuffer = apResponse->getResponseData();  
  89.     std::string header(headerBuffer->begin(), headerBuffer->end());  
  90.     std::string body(bodyBuffer->begin(), bodyBuffer->end());  
  91.     pStack->pushCCObject(apResponse->getHttpRequest(), "CCHttpRequest");  
  92.     pStack->pushBoolean(isSucceed); //是否成功  
  93.     pStack->pushString(body.c_str(), body.size()); //body  
  94.     pStack->pushString(header.c_str()); //header  
  95.     pStack->pushInt(status); //状态码  
  96.     pStack->pushString(errorBuffer); //错误描述  
  97.     pStack->executeFunctionByHandler(mHandler, 6);  
  98.     pStack->clean();  
  99. }  

注意使用tolua++的时候需要注意一下-L basic.lua

最终使用的lua代码如下

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. local request = CCLuaHttpRequest:create()  
  2.         request:setUrl(url)  
  3.         request:setTag(tag)  
  4.         request:setRequestType(CCHttpRequest.kHttpGet)  
  5.         request:setResponseScriptCallback(dlImageCallback)  
  6.         CCHttpClient:getInstance():send(request)  
  7.         request:release()  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值