mirror of https://github.com/axmolengine/axmol.git
Merge pull request #6263 from Mazyod/develop-cpp11-http-client
closed #4702: Update HTTPClient to use C++11 callbacks
This commit is contained in:
commit
328408d792
|
@ -502,10 +502,15 @@ void HttpClient::dispatchResponseCallbacks()
|
|||
if (response)
|
||||
{
|
||||
HttpRequest *request = response->getHttpRequest();
|
||||
const ccHttpRequestCallback& callback = request->getCallback();
|
||||
Ref* pTarget = request->getTarget();
|
||||
SEL_HttpResponse pSelector = request->getSelector();
|
||||
|
||||
if (pTarget && pSelector)
|
||||
if (callback != nullptr)
|
||||
{
|
||||
callback(this, response);
|
||||
}
|
||||
else if (pTarget && pSelector)
|
||||
{
|
||||
(pTarget->*pSelector)(this, response);
|
||||
}
|
||||
|
|
|
@ -38,6 +38,7 @@ namespace network {
|
|||
class HttpClient;
|
||||
class HttpResponse;
|
||||
|
||||
typedef std::function<void(HttpClient* client, HttpResponse* response)> ccHttpRequestCallback;
|
||||
typedef void (cocos2d::Ref::*SEL_HttpResponse)(HttpClient* client, HttpResponse* response);
|
||||
#define httpresponse_selector(_SELECTOR) (cocos2d::network::SEL_HttpResponse)(&_SELECTOR)
|
||||
|
||||
|
@ -78,9 +79,10 @@ public:
|
|||
_url.clear();
|
||||
_requestData.clear();
|
||||
_tag.clear();
|
||||
_pTarget = NULL;
|
||||
_pSelector = NULL;
|
||||
_pUserData = NULL;
|
||||
_pTarget = nullptr;
|
||||
_pSelector = nullptr;
|
||||
_pCallback = nullptr;
|
||||
_pUserData = nullptr;
|
||||
};
|
||||
|
||||
/** Destructor */
|
||||
|
@ -183,7 +185,7 @@ public:
|
|||
setResponseCallback(pTarget, (SEL_HttpResponse) pSelector);
|
||||
}
|
||||
|
||||
inline void setResponseCallback(Ref* pTarget, SEL_HttpResponse pSelector)
|
||||
CC_DEPRECATED_ATTRIBUTE inline void setResponseCallback(Ref* pTarget, SEL_HttpResponse pSelector)
|
||||
{
|
||||
_pTarget = pTarget;
|
||||
_pSelector = pSelector;
|
||||
|
@ -192,7 +194,13 @@ public:
|
|||
{
|
||||
_pTarget->retain();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline void setResponseCallback(const ccHttpRequestCallback& callback)
|
||||
{
|
||||
_pCallback = callback;
|
||||
}
|
||||
|
||||
/** Get the target of callback selector funtion, mainly used by HttpClient */
|
||||
inline Ref* getTarget()
|
||||
{
|
||||
|
@ -218,6 +226,11 @@ public:
|
|||
return _prxy(_pSelector);
|
||||
}
|
||||
|
||||
inline const ccHttpRequestCallback& getCallback()
|
||||
{
|
||||
return _pCallback;
|
||||
}
|
||||
|
||||
/** Set any custom headers **/
|
||||
inline void setHeaders(std::vector<std::string> pHeaders)
|
||||
{
|
||||
|
@ -238,6 +251,7 @@ protected:
|
|||
std::string _tag; /// user defined tag, to identify different requests in response callback
|
||||
Ref* _pTarget; /// callback target of pSelector function
|
||||
SEL_HttpResponse _pSelector; /// callback function, e.g. MyLayer::onHttpResponse(HttpClient *sender, HttpResponse * response)
|
||||
ccHttpRequestCallback _pCallback; /// C++11 style callbacks
|
||||
void* _pUserData; /// You can add your customed data here
|
||||
std::vector<std::string> _headers; /// custom http headers
|
||||
};
|
||||
|
|
|
@ -77,7 +77,7 @@ void HttpClientTest::onMenuGetTestClicked(cocos2d::Ref *sender)
|
|||
HttpRequest* request = new HttpRequest();
|
||||
request->setUrl("http://just-make-this-request-failed.com");
|
||||
request->setRequestType(HttpRequest::Type::GET);
|
||||
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
||||
request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
|
||||
request->setTag("GET test1");
|
||||
HttpClient::getInstance()->send(request);
|
||||
request->release();
|
||||
|
@ -89,7 +89,7 @@ void HttpClientTest::onMenuGetTestClicked(cocos2d::Ref *sender)
|
|||
// required fields
|
||||
request->setUrl("http://httpbin.org/ip");
|
||||
request->setRequestType(HttpRequest::Type::GET);
|
||||
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
||||
request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
|
||||
// optional fields
|
||||
request->setTag("GET test2");
|
||||
|
||||
|
@ -104,7 +104,7 @@ void HttpClientTest::onMenuGetTestClicked(cocos2d::Ref *sender)
|
|||
HttpRequest* request = new HttpRequest();
|
||||
request->setUrl("https://httpbin.org/get");
|
||||
request->setRequestType(HttpRequest::Type::GET);
|
||||
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
||||
request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
|
||||
request->setTag("GET test3");
|
||||
HttpClient::getInstance()->send(request);
|
||||
request->release();
|
||||
|
@ -122,7 +122,7 @@ void HttpClientTest::onMenuPostTestClicked(cocos2d::Ref *sender)
|
|||
HttpRequest* request = new HttpRequest();
|
||||
request->setUrl("http://httpbin.org/post");
|
||||
request->setRequestType(HttpRequest::Type::POST);
|
||||
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
||||
request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
|
||||
|
||||
// write the post data
|
||||
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
|
||||
|
@ -141,7 +141,7 @@ void HttpClientTest::onMenuPostTestClicked(cocos2d::Ref *sender)
|
|||
std::vector<std::string> headers;
|
||||
headers.push_back("Content-Type: application/json; charset=utf-8");
|
||||
request->setHeaders(headers);
|
||||
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
||||
request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
|
||||
|
||||
// write the post data
|
||||
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
|
||||
|
@ -161,7 +161,7 @@ void HttpClientTest::onMenuPostBinaryTestClicked(cocos2d::Ref *sender)
|
|||
HttpRequest* request = new HttpRequest();
|
||||
request->setUrl("http://httpbin.org/post");
|
||||
request->setRequestType(HttpRequest::Type::POST);
|
||||
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
||||
request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
|
||||
|
||||
// write the post data
|
||||
char postData[22] = "binary=hello\0\0cocos2d"; // including \0, the strings after \0 should not be cut in response
|
||||
|
@ -184,7 +184,7 @@ void HttpClientTest::onMenuPutTestClicked(Ref *sender)
|
|||
HttpRequest* request = new HttpRequest();
|
||||
request->setUrl("http://httpbin.org/put");
|
||||
request->setRequestType(HttpRequest::Type::PUT);
|
||||
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
||||
request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
|
||||
|
||||
// write the post data
|
||||
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
|
||||
|
@ -203,7 +203,7 @@ void HttpClientTest::onMenuPutTestClicked(Ref *sender)
|
|||
std::vector<std::string> headers;
|
||||
headers.push_back("Content-Type: application/json; charset=utf-8");
|
||||
request->setHeaders(headers);
|
||||
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
||||
request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
|
||||
|
||||
// write the post data
|
||||
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
|
||||
|
@ -225,7 +225,7 @@ void HttpClientTest::onMenuDeleteTestClicked(Ref *sender)
|
|||
HttpRequest* request = new HttpRequest();
|
||||
request->setUrl("http://just-make-this-request-failed.com");
|
||||
request->setRequestType(HttpRequest::Type::DELETE);
|
||||
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
||||
request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
|
||||
request->setTag("DELETE test1");
|
||||
HttpClient::getInstance()->send(request);
|
||||
request->release();
|
||||
|
@ -236,7 +236,7 @@ void HttpClientTest::onMenuDeleteTestClicked(Ref *sender)
|
|||
HttpRequest* request = new HttpRequest();
|
||||
request->setUrl("http://httpbin.org/delete");
|
||||
request->setRequestType(HttpRequest::Type::DELETE);
|
||||
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
|
||||
request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
|
||||
request->setTag("DELETE test2");
|
||||
HttpClient::getInstance()->send(request);
|
||||
request->release();
|
||||
|
|
Loading…
Reference in New Issue