mirror of https://github.com/axmolengine/axmol.git
commit
e0e9d3379c
|
@ -95,12 +95,12 @@ static size_t writeHeaderData(void *ptr, size_t size, size_t nmemb, void *stream
|
|||
}
|
||||
|
||||
|
||||
static int processGetTask(HttpRequest *request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream);
|
||||
static int processPostTask(HttpRequest *request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream);
|
||||
static int processPutTask(HttpRequest *request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream);
|
||||
static int processDeleteTask(HttpRequest *request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream);
|
||||
static int processGetTask(HttpRequest *request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream, char *errorBuffer);
|
||||
static int processPostTask(HttpRequest *request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream, char *errorBuffer);
|
||||
static int processPutTask(HttpRequest *request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream, char *errorBuffer);
|
||||
static int processDeleteTask(HttpRequest *request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream, char *errorBuffer);
|
||||
// int processDownloadTask(HttpRequest *task, write_callback callback, void *stream, int32_t *errorCode);
|
||||
|
||||
static void processResponse(HttpResponse* response, char* errorBuffer);
|
||||
|
||||
// Worker thread
|
||||
void HttpClient::networkThread()
|
||||
|
@ -144,69 +144,7 @@ void HttpClient::networkThread()
|
|||
// Create a HttpResponse object, the default setting is http access failed
|
||||
HttpResponse *response = new HttpResponse(request);
|
||||
|
||||
// request's refcount = 2 here, it's retained by HttpRespose constructor
|
||||
request->release();
|
||||
// ok, refcount = 1 now, only HttpResponse hold it.
|
||||
|
||||
long responseCode = -1;
|
||||
int retValue = 0;
|
||||
|
||||
// Process the request -> get response packet
|
||||
switch (request->getRequestType())
|
||||
{
|
||||
case HttpRequest::Type::GET: // HTTP GET
|
||||
retValue = processGetTask(request,
|
||||
writeData,
|
||||
response->getResponseData(),
|
||||
&responseCode,
|
||||
writeHeaderData,
|
||||
response->getResponseHeader());
|
||||
break;
|
||||
|
||||
case HttpRequest::Type::POST: // HTTP POST
|
||||
retValue = processPostTask(request,
|
||||
writeData,
|
||||
response->getResponseData(),
|
||||
&responseCode,
|
||||
writeHeaderData,
|
||||
response->getResponseHeader());
|
||||
break;
|
||||
|
||||
case HttpRequest::Type::PUT:
|
||||
retValue = processPutTask(request,
|
||||
writeData,
|
||||
response->getResponseData(),
|
||||
&responseCode,
|
||||
writeHeaderData,
|
||||
response->getResponseHeader());
|
||||
break;
|
||||
|
||||
case HttpRequest::Type::DELETE:
|
||||
retValue = processDeleteTask(request,
|
||||
writeData,
|
||||
response->getResponseData(),
|
||||
&responseCode,
|
||||
writeHeaderData,
|
||||
response->getResponseHeader());
|
||||
break;
|
||||
|
||||
default:
|
||||
CCASSERT(true, "CCHttpClient: unkown request type, only GET and POSt are supported");
|
||||
break;
|
||||
}
|
||||
|
||||
// write data to HttpResponse
|
||||
response->setResponseCode(responseCode);
|
||||
|
||||
if (retValue != 0)
|
||||
{
|
||||
response->setSucceed(false);
|
||||
response->setErrorBuffer(s_errorBuffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
response->setSucceed(true);
|
||||
}
|
||||
processResponse(response, s_errorBuffer);
|
||||
|
||||
|
||||
// add response packet into queue
|
||||
|
@ -234,15 +172,43 @@ void HttpClient::networkThread()
|
|||
|
||||
}
|
||||
|
||||
// Worker thread
|
||||
void HttpClient::networkThreadAlone(HttpRequest* request)
|
||||
{
|
||||
// Create a HttpResponse object, the default setting is http access failed
|
||||
HttpResponse *response = new HttpResponse(request);
|
||||
char errorBuffer[CURL_ERROR_SIZE] = { 0 };
|
||||
processResponse(response, errorBuffer);
|
||||
|
||||
auto scheduler = Director::getInstance()->getScheduler();
|
||||
scheduler->performFunctionInCocosThread([response, request]{
|
||||
const ccHttpRequestCallback& callback = request->getCallback();
|
||||
Ref* pTarget = request->getTarget();
|
||||
SEL_HttpResponse pSelector = request->getSelector();
|
||||
|
||||
if (callback != nullptr)
|
||||
{
|
||||
callback(s_pHttpClient, response);
|
||||
}
|
||||
else if (pTarget && pSelector)
|
||||
{
|
||||
(pTarget->*pSelector)(s_pHttpClient, response);
|
||||
}
|
||||
response->release();
|
||||
// do not release in other thread
|
||||
request->release();
|
||||
});
|
||||
}
|
||||
|
||||
//Configure curl's timeout property
|
||||
static bool configureCURL(CURL *handle)
|
||||
static bool configureCURL(CURL *handle, char *errorBuffer)
|
||||
{
|
||||
if (!handle) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int32_t code;
|
||||
code = curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, s_errorBuffer);
|
||||
code = curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errorBuffer);
|
||||
if (code != CURLE_OK) {
|
||||
return false;
|
||||
}
|
||||
|
@ -298,11 +264,11 @@ public:
|
|||
* @param callback Response write callback
|
||||
* @param stream Response write stream
|
||||
*/
|
||||
bool init(HttpRequest *request, write_callback callback, void *stream, write_callback headerCallback, void *headerStream)
|
||||
bool init(HttpRequest *request, write_callback callback, void *stream, write_callback headerCallback, void *headerStream, char *errorBuffer)
|
||||
{
|
||||
if (!_curl)
|
||||
return false;
|
||||
if (!configureCURL(_curl))
|
||||
if (!configureCURL(_curl, errorBuffer))
|
||||
return false;
|
||||
|
||||
/* get custom header data (if set) */
|
||||
|
@ -350,20 +316,20 @@ public:
|
|||
};
|
||||
|
||||
//Process Get Request
|
||||
static int processGetTask(HttpRequest *request, write_callback callback, void *stream, long *responseCode, write_callback headerCallback, void *headerStream)
|
||||
static int processGetTask(HttpRequest *request, write_callback callback, void *stream, long *responseCode, write_callback headerCallback, void *headerStream, char *errorBuffer)
|
||||
{
|
||||
CURLRaii curl;
|
||||
bool ok = curl.init(request, callback, stream, headerCallback, headerStream)
|
||||
bool ok = curl.init(request, callback, stream, headerCallback, headerStream, errorBuffer)
|
||||
&& curl.setOption(CURLOPT_FOLLOWLOCATION, true)
|
||||
&& curl.perform(responseCode);
|
||||
return ok ? 0 : 1;
|
||||
}
|
||||
|
||||
//Process POST Request
|
||||
static int processPostTask(HttpRequest *request, write_callback callback, void *stream, long *responseCode, write_callback headerCallback, void *headerStream)
|
||||
static int processPostTask(HttpRequest *request, write_callback callback, void *stream, long *responseCode, write_callback headerCallback, void *headerStream, char *errorBuffer)
|
||||
{
|
||||
CURLRaii curl;
|
||||
bool ok = curl.init(request, callback, stream, headerCallback, headerStream)
|
||||
bool ok = curl.init(request, callback, stream, headerCallback, headerStream, errorBuffer)
|
||||
&& curl.setOption(CURLOPT_POST, 1)
|
||||
&& curl.setOption(CURLOPT_POSTFIELDS, request->getRequestData())
|
||||
&& curl.setOption(CURLOPT_POSTFIELDSIZE, request->getRequestDataSize())
|
||||
|
@ -372,10 +338,10 @@ static int processPostTask(HttpRequest *request, write_callback callback, void *
|
|||
}
|
||||
|
||||
//Process PUT Request
|
||||
static int processPutTask(HttpRequest *request, write_callback callback, void *stream, long *responseCode, write_callback headerCallback, void *headerStream)
|
||||
static int processPutTask(HttpRequest *request, write_callback callback, void *stream, long *responseCode, write_callback headerCallback, void *headerStream, char *errorBuffer)
|
||||
{
|
||||
CURLRaii curl;
|
||||
bool ok = curl.init(request, callback, stream, headerCallback, headerStream)
|
||||
bool ok = curl.init(request, callback, stream, headerCallback, headerStream, errorBuffer)
|
||||
&& curl.setOption(CURLOPT_CUSTOMREQUEST, "PUT")
|
||||
&& curl.setOption(CURLOPT_POSTFIELDS, request->getRequestData())
|
||||
&& curl.setOption(CURLOPT_POSTFIELDSIZE, request->getRequestDataSize())
|
||||
|
@ -384,16 +350,86 @@ static int processPutTask(HttpRequest *request, write_callback callback, void *s
|
|||
}
|
||||
|
||||
//Process DELETE Request
|
||||
static int processDeleteTask(HttpRequest *request, write_callback callback, void *stream, long *responseCode, write_callback headerCallback, void *headerStream)
|
||||
static int processDeleteTask(HttpRequest *request, write_callback callback, void *stream, long *responseCode, write_callback headerCallback, void *headerStream, char *errorBuffer)
|
||||
{
|
||||
CURLRaii curl;
|
||||
bool ok = curl.init(request, callback, stream, headerCallback, headerStream)
|
||||
bool ok = curl.init(request, callback, stream, headerCallback, headerStream, errorBuffer)
|
||||
&& curl.setOption(CURLOPT_CUSTOMREQUEST, "DELETE")
|
||||
&& curl.setOption(CURLOPT_FOLLOWLOCATION, true)
|
||||
&& curl.perform(responseCode);
|
||||
return ok ? 0 : 1;
|
||||
}
|
||||
|
||||
|
||||
// Process Response
|
||||
static void processResponse(HttpResponse* response, char* errorBuffer)
|
||||
{
|
||||
auto request = response->getHttpRequest();
|
||||
long responseCode = -1;
|
||||
int retValue = 0;
|
||||
|
||||
// Process the request -> get response packet
|
||||
switch (request->getRequestType())
|
||||
{
|
||||
case HttpRequest::Type::GET: // HTTP GET
|
||||
retValue = processGetTask(request,
|
||||
writeData,
|
||||
response->getResponseData(),
|
||||
&responseCode,
|
||||
writeHeaderData,
|
||||
response->getResponseHeader(),
|
||||
errorBuffer);
|
||||
break;
|
||||
|
||||
case HttpRequest::Type::POST: // HTTP POST
|
||||
retValue = processPostTask(request,
|
||||
writeData,
|
||||
response->getResponseData(),
|
||||
&responseCode,
|
||||
writeHeaderData,
|
||||
response->getResponseHeader(),
|
||||
errorBuffer);
|
||||
break;
|
||||
|
||||
case HttpRequest::Type::PUT:
|
||||
retValue = processPutTask(request,
|
||||
writeData,
|
||||
response->getResponseData(),
|
||||
&responseCode,
|
||||
writeHeaderData,
|
||||
response->getResponseHeader(),
|
||||
errorBuffer);
|
||||
break;
|
||||
|
||||
case HttpRequest::Type::DELETE:
|
||||
retValue = processDeleteTask(request,
|
||||
writeData,
|
||||
response->getResponseData(),
|
||||
&responseCode,
|
||||
writeHeaderData,
|
||||
response->getResponseHeader(),
|
||||
errorBuffer);
|
||||
break;
|
||||
|
||||
default:
|
||||
CCASSERT(true, "CCHttpClient: unkown request type, only GET and POSt are supported");
|
||||
break;
|
||||
}
|
||||
|
||||
// write data to HttpResponse
|
||||
response->setResponseCode(responseCode);
|
||||
|
||||
if (retValue != 0)
|
||||
{
|
||||
response->setSucceed(false);
|
||||
response->setErrorBuffer(errorBuffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
response->setSucceed(true);
|
||||
}
|
||||
}
|
||||
|
||||
// HttpClient implementation
|
||||
HttpClient* HttpClient::getInstance()
|
||||
{
|
||||
|
@ -445,10 +481,10 @@ bool HttpClient::lazyInitThreadSemphore()
|
|||
s_requestQueue = new Vector<HttpRequest*>();
|
||||
s_responseQueue = new Vector<HttpResponse*>();
|
||||
|
||||
s_need_quit = false;
|
||||
|
||||
auto t = std::thread(CC_CALLBACK_0(HttpClient::networkThread, this));
|
||||
t.detach();
|
||||
|
||||
s_need_quit = false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -479,6 +515,18 @@ void HttpClient::send(HttpRequest* request)
|
|||
}
|
||||
}
|
||||
|
||||
void HttpClient::sendImmediate(HttpRequest* request)
|
||||
{
|
||||
if(!request)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
request->retain();
|
||||
auto t = std::thread(&HttpClient::networkThreadAlone, this, request);
|
||||
t.detach();
|
||||
}
|
||||
|
||||
// Poll and notify main thread if responses exists in queue
|
||||
void HttpClient::dispatchResponseCallbacks()
|
||||
{
|
||||
|
@ -516,6 +564,8 @@ void HttpClient::dispatchResponseCallbacks()
|
|||
}
|
||||
|
||||
response->release();
|
||||
// do not release in other thread
|
||||
request->release();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -63,6 +63,13 @@ public:
|
|||
*/
|
||||
void send(HttpRequest* request);
|
||||
|
||||
/**
|
||||
* Immediate send a request
|
||||
* @param request a HttpRequest object, which includes url, response callback etc.
|
||||
please make sure request->_requestData is clear before calling "sendImmediate" here.
|
||||
*/
|
||||
void sendImmediate(HttpRequest* request);
|
||||
|
||||
|
||||
/**
|
||||
* Change the connect timeout
|
||||
|
@ -101,6 +108,7 @@ private:
|
|||
*/
|
||||
bool lazyInitThreadSemphore();
|
||||
void networkThread();
|
||||
void networkThreadAlone(HttpRequest* request);
|
||||
/** Poll function called from main thread to dispatch callbacks when http requests finished **/
|
||||
void dispatchResponseCallbacks();
|
||||
|
||||
|
|
|
@ -14,6 +14,9 @@ HttpClientTest::HttpClientTest()
|
|||
const int MARGIN = 40;
|
||||
const int SPACE = 35;
|
||||
|
||||
const int LEFT = winSize.width / 4;
|
||||
const int RIGHT = winSize.width / 4 * 3;
|
||||
|
||||
auto label = Label::createWithTTF("Http Request Test", "fonts/arial.ttf", 28);
|
||||
label->setPosition(Vec2(winSize.width / 2, winSize.height - MARGIN));
|
||||
addChild(label, 0);
|
||||
|
@ -24,36 +27,66 @@ HttpClientTest::HttpClientTest()
|
|||
|
||||
// Get
|
||||
auto labelGet = Label::createWithTTF("Test Get", "fonts/arial.ttf", 22);
|
||||
auto itemGet = MenuItemLabel::create(labelGet, CC_CALLBACK_1(HttpClientTest::onMenuGetTestClicked, this));
|
||||
itemGet->setPosition(Vec2(winSize.width / 2, winSize.height - MARGIN - SPACE));
|
||||
auto itemGet = MenuItemLabel::create(labelGet, CC_CALLBACK_1(HttpClientTest::onMenuGetTestClicked, this, false));
|
||||
itemGet->setPosition(Vec2(LEFT, winSize.height - MARGIN - SPACE));
|
||||
menuRequest->addChild(itemGet);
|
||||
|
||||
// Post
|
||||
auto labelPost = Label::createWithTTF("Test Post", "fonts/arial.ttf", 22);
|
||||
auto itemPost = MenuItemLabel::create(labelPost, CC_CALLBACK_1(HttpClientTest::onMenuPostTestClicked, this));
|
||||
itemPost->setPosition(Vec2(winSize.width / 2, winSize.height - MARGIN - 2 * SPACE));
|
||||
auto itemPost = MenuItemLabel::create(labelPost, CC_CALLBACK_1(HttpClientTest::onMenuPostTestClicked, this, false));
|
||||
itemPost->setPosition(Vec2(LEFT, winSize.height - MARGIN - 2 * SPACE));
|
||||
menuRequest->addChild(itemPost);
|
||||
|
||||
// Post Binary
|
||||
auto labelPostBinary = Label::createWithTTF("Test Post Binary", "fonts/arial.ttf", 22);
|
||||
auto itemPostBinary = MenuItemLabel::create(labelPostBinary, CC_CALLBACK_1(HttpClientTest::onMenuPostBinaryTestClicked, this));
|
||||
itemPostBinary->setPosition(Vec2(winSize.width / 2, winSize.height - MARGIN - 3 * SPACE));
|
||||
auto itemPostBinary = MenuItemLabel::create(labelPostBinary, CC_CALLBACK_1(HttpClientTest::onMenuPostBinaryTestClicked, this, false));
|
||||
itemPostBinary->setPosition(Vec2(LEFT, winSize.height - MARGIN - 3 * SPACE));
|
||||
menuRequest->addChild(itemPostBinary);
|
||||
|
||||
// Put
|
||||
auto labelPut = Label::createWithTTF("Test Put", "fonts/arial.ttf", 22);
|
||||
auto itemPut = MenuItemLabel::create(labelPut, CC_CALLBACK_1(HttpClientTest::onMenuPutTestClicked, this));
|
||||
itemPut->setPosition(Vec2(winSize.width / 2, winSize.height - MARGIN - 4 * SPACE));
|
||||
auto itemPut = MenuItemLabel::create(labelPut, CC_CALLBACK_1(HttpClientTest::onMenuPutTestClicked, this, false));
|
||||
itemPut->setPosition(Vec2(LEFT, winSize.height - MARGIN - 4 * SPACE));
|
||||
menuRequest->addChild(itemPut);
|
||||
|
||||
// Delete
|
||||
auto labelDelete = Label::createWithTTF("Test Delete", "fonts/arial.ttf", 22);
|
||||
auto itemDelete = MenuItemLabel::create(labelDelete, CC_CALLBACK_1(HttpClientTest::onMenuDeleteTestClicked, this));
|
||||
itemDelete->setPosition(Vec2(winSize.width / 2, winSize.height - MARGIN - 5 * SPACE));
|
||||
auto itemDelete = MenuItemLabel::create(labelDelete, CC_CALLBACK_1(HttpClientTest::onMenuDeleteTestClicked, this, false));
|
||||
itemDelete->setPosition(Vec2(LEFT, winSize.height - MARGIN - 5 * SPACE));
|
||||
menuRequest->addChild(itemDelete);
|
||||
|
||||
// Get for sendImmediate
|
||||
labelGet = Label::createWithTTF("Test Immediate Get", "fonts/arial.ttf", 22);
|
||||
itemGet = MenuItemLabel::create(labelGet, CC_CALLBACK_1(HttpClientTest::onMenuGetTestClicked, this, true));
|
||||
itemGet->setPosition(Vec2(RIGHT, winSize.height - MARGIN - SPACE));
|
||||
menuRequest->addChild(itemGet);
|
||||
|
||||
// Post for sendImmediate
|
||||
labelPost = Label::createWithTTF("Test Immediate Post", "fonts/arial.ttf", 22);
|
||||
itemPost = MenuItemLabel::create(labelPost, CC_CALLBACK_1(HttpClientTest::onMenuPostTestClicked, this, true));
|
||||
itemPost->setPosition(Vec2(RIGHT, winSize.height - MARGIN - 2 * SPACE));
|
||||
menuRequest->addChild(itemPost);
|
||||
|
||||
// Post Binary for sendImmediate
|
||||
labelPostBinary = Label::createWithTTF("Test Immediate Post Binary", "fonts/arial.ttf", 22);
|
||||
itemPostBinary = MenuItemLabel::create(labelPostBinary, CC_CALLBACK_1(HttpClientTest::onMenuPostBinaryTestClicked, this, true));
|
||||
itemPostBinary->setPosition(Vec2(RIGHT, winSize.height - MARGIN - 3 * SPACE));
|
||||
menuRequest->addChild(itemPostBinary);
|
||||
|
||||
// Put for sendImmediate
|
||||
labelPut = Label::createWithTTF("Test Immediate Put", "fonts/arial.ttf", 22);
|
||||
itemPut = MenuItemLabel::create(labelPut, CC_CALLBACK_1(HttpClientTest::onMenuPutTestClicked, this, true));
|
||||
itemPut->setPosition(Vec2(RIGHT, winSize.height - MARGIN - 4 * SPACE));
|
||||
menuRequest->addChild(itemPut);
|
||||
|
||||
// Delete for sendImmediate
|
||||
labelDelete = Label::createWithTTF("Test Immediate Delete", "fonts/arial.ttf", 22);
|
||||
itemDelete = MenuItemLabel::create(labelDelete, CC_CALLBACK_1(HttpClientTest::onMenuDeleteTestClicked, this, true));
|
||||
itemDelete->setPosition(Vec2(RIGHT, winSize.height - MARGIN - 5 * SPACE));
|
||||
menuRequest->addChild(itemDelete);
|
||||
|
||||
// Response Code Label
|
||||
_labelStatusCode = Label::createWithTTF("HTTP Status Code", "fonts/arial.ttf", 22);
|
||||
_labelStatusCode = Label::createWithTTF("HTTP Status Code", "fonts/arial.ttf", 18);
|
||||
_labelStatusCode->setPosition(Vec2(winSize.width / 2, winSize.height - MARGIN - 6 * SPACE));
|
||||
addChild(_labelStatusCode);
|
||||
|
||||
|
@ -70,7 +103,7 @@ HttpClientTest::~HttpClientTest()
|
|||
HttpClient::destroyInstance();
|
||||
}
|
||||
|
||||
void HttpClientTest::onMenuGetTestClicked(cocos2d::Ref *sender)
|
||||
void HttpClientTest::onMenuGetTestClicked(cocos2d::Ref *sender, bool isImmediate)
|
||||
{
|
||||
// test 1
|
||||
{
|
||||
|
@ -78,8 +111,15 @@ void HttpClientTest::onMenuGetTestClicked(cocos2d::Ref *sender)
|
|||
request->setUrl("http://just-make-this-request-failed.com");
|
||||
request->setRequestType(HttpRequest::Type::GET);
|
||||
request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
|
||||
if (isImmediate)
|
||||
{
|
||||
request->setTag("GET immediate test1");
|
||||
HttpClient::getInstance()->sendImmediate(request);
|
||||
}else
|
||||
{
|
||||
request->setTag("GET test1");
|
||||
HttpClient::getInstance()->send(request);
|
||||
}
|
||||
request->release();
|
||||
}
|
||||
|
||||
|
@ -90,11 +130,15 @@ void HttpClientTest::onMenuGetTestClicked(cocos2d::Ref *sender)
|
|||
request->setUrl("http://httpbin.org/ip");
|
||||
request->setRequestType(HttpRequest::Type::GET);
|
||||
request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
|
||||
// optional fields
|
||||
if (isImmediate)
|
||||
{
|
||||
request->setTag("GET immediate test2");
|
||||
HttpClient::getInstance()->sendImmediate(request);
|
||||
}else
|
||||
{
|
||||
request->setTag("GET test2");
|
||||
|
||||
HttpClient::getInstance()->send(request);
|
||||
|
||||
}
|
||||
// don't forget to release it, pair to new
|
||||
request->release();
|
||||
}
|
||||
|
@ -105,8 +149,15 @@ void HttpClientTest::onMenuGetTestClicked(cocos2d::Ref *sender)
|
|||
request->setUrl("https://httpbin.org/get");
|
||||
request->setRequestType(HttpRequest::Type::GET);
|
||||
request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
|
||||
if (isImmediate)
|
||||
{
|
||||
request->setTag("GET immediate test3");
|
||||
HttpClient::getInstance()->sendImmediate(request);
|
||||
}else
|
||||
{
|
||||
request->setTag("GET test3");
|
||||
HttpClient::getInstance()->send(request);
|
||||
}
|
||||
request->release();
|
||||
}
|
||||
|
||||
|
@ -115,7 +166,7 @@ void HttpClientTest::onMenuGetTestClicked(cocos2d::Ref *sender)
|
|||
|
||||
}
|
||||
|
||||
void HttpClientTest::onMenuPostTestClicked(cocos2d::Ref *sender)
|
||||
void HttpClientTest::onMenuPostTestClicked(cocos2d::Ref *sender, bool isImmediate)
|
||||
{
|
||||
// test 1
|
||||
{
|
||||
|
@ -127,9 +178,15 @@ void HttpClientTest::onMenuPostTestClicked(cocos2d::Ref *sender)
|
|||
// write the post data
|
||||
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
|
||||
request->setRequestData(postData, strlen(postData));
|
||||
|
||||
if (isImmediate)
|
||||
{
|
||||
request->setTag("POST immediate test1");
|
||||
HttpClient::getInstance()->sendImmediate(request);
|
||||
}else
|
||||
{
|
||||
request->setTag("POST test1");
|
||||
HttpClient::getInstance()->send(request);
|
||||
}
|
||||
request->release();
|
||||
}
|
||||
|
||||
|
@ -146,9 +203,15 @@ void HttpClientTest::onMenuPostTestClicked(cocos2d::Ref *sender)
|
|||
// write the post data
|
||||
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
|
||||
request->setRequestData(postData, strlen(postData));
|
||||
|
||||
if (isImmediate)
|
||||
{
|
||||
request->setTag("POST immediate test2");
|
||||
HttpClient::getInstance()->sendImmediate(request);
|
||||
}else
|
||||
{
|
||||
request->setTag("POST test2");
|
||||
HttpClient::getInstance()->send(request);
|
||||
}
|
||||
request->release();
|
||||
}
|
||||
|
||||
|
@ -156,7 +219,7 @@ void HttpClientTest::onMenuPostTestClicked(cocos2d::Ref *sender)
|
|||
_labelStatusCode->setString("waiting...");
|
||||
}
|
||||
|
||||
void HttpClientTest::onMenuPostBinaryTestClicked(cocos2d::Ref *sender)
|
||||
void HttpClientTest::onMenuPostBinaryTestClicked(cocos2d::Ref *sender, bool isImmediate)
|
||||
{
|
||||
HttpRequest* request = new HttpRequest();
|
||||
request->setUrl("http://httpbin.org/post");
|
||||
|
@ -166,9 +229,15 @@ void HttpClientTest::onMenuPostBinaryTestClicked(cocos2d::Ref *sender)
|
|||
// write the post data
|
||||
char postData[22] = "binary=hello\0\0cocos2d"; // including \0, the strings after \0 should not be cut in response
|
||||
request->setRequestData(postData, 22);
|
||||
|
||||
if (isImmediate)
|
||||
{
|
||||
request->setTag("POST Binary immediate test");
|
||||
HttpClient::getInstance()->sendImmediate(request);
|
||||
}else
|
||||
{
|
||||
request->setTag("POST Binary test");
|
||||
HttpClient::getInstance()->send(request);
|
||||
}
|
||||
request->release();
|
||||
|
||||
// waiting
|
||||
|
@ -177,7 +246,7 @@ void HttpClientTest::onMenuPostBinaryTestClicked(cocos2d::Ref *sender)
|
|||
|
||||
|
||||
|
||||
void HttpClientTest::onMenuPutTestClicked(Ref *sender)
|
||||
void HttpClientTest::onMenuPutTestClicked(Ref *sender, bool isImmediate)
|
||||
{
|
||||
// test 1
|
||||
{
|
||||
|
@ -189,9 +258,15 @@ void HttpClientTest::onMenuPutTestClicked(Ref *sender)
|
|||
// write the post data
|
||||
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
|
||||
request->setRequestData(postData, strlen(postData));
|
||||
|
||||
request->setTag("PUT test1");
|
||||
if (isImmediate)
|
||||
{
|
||||
request->setTag("PUT Binary immediate test1");
|
||||
HttpClient::getInstance()->sendImmediate(request);
|
||||
}else
|
||||
{
|
||||
request->setTag("PUT Binary test1");
|
||||
HttpClient::getInstance()->send(request);
|
||||
}
|
||||
request->release();
|
||||
}
|
||||
|
||||
|
@ -208,9 +283,15 @@ void HttpClientTest::onMenuPutTestClicked(Ref *sender)
|
|||
// write the post data
|
||||
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
|
||||
request->setRequestData(postData, strlen(postData));
|
||||
|
||||
request->setTag("PUT test2");
|
||||
if (isImmediate)
|
||||
{
|
||||
request->setTag("PUT Binary immediate test2");
|
||||
HttpClient::getInstance()->sendImmediate(request);
|
||||
}else
|
||||
{
|
||||
request->setTag("PUT Binary test2");
|
||||
HttpClient::getInstance()->send(request);
|
||||
}
|
||||
request->release();
|
||||
}
|
||||
|
||||
|
@ -218,7 +299,7 @@ void HttpClientTest::onMenuPutTestClicked(Ref *sender)
|
|||
_labelStatusCode->setString("waiting...");
|
||||
}
|
||||
|
||||
void HttpClientTest::onMenuDeleteTestClicked(Ref *sender)
|
||||
void HttpClientTest::onMenuDeleteTestClicked(Ref *sender, bool isImmediate)
|
||||
{
|
||||
// test 1
|
||||
{
|
||||
|
@ -226,8 +307,15 @@ void HttpClientTest::onMenuDeleteTestClicked(Ref *sender)
|
|||
request->setUrl("http://just-make-this-request-failed.com");
|
||||
request->setRequestType(HttpRequest::Type::DELETE);
|
||||
request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
|
||||
if (isImmediate)
|
||||
{
|
||||
request->setTag("DELETE immediate test1");
|
||||
HttpClient::getInstance()->sendImmediate(request);
|
||||
}else
|
||||
{
|
||||
request->setTag("DELETE test1");
|
||||
HttpClient::getInstance()->send(request);
|
||||
}
|
||||
request->release();
|
||||
}
|
||||
|
||||
|
@ -237,8 +325,15 @@ void HttpClientTest::onMenuDeleteTestClicked(Ref *sender)
|
|||
request->setUrl("http://httpbin.org/delete");
|
||||
request->setRequestType(HttpRequest::Type::DELETE);
|
||||
request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
|
||||
if (isImmediate)
|
||||
{
|
||||
request->setTag("DELETE immediate test2");
|
||||
HttpClient::getInstance()->sendImmediate(request);
|
||||
}else
|
||||
{
|
||||
request->setTag("DELETE test2");
|
||||
HttpClient::getInstance()->send(request);
|
||||
}
|
||||
request->release();
|
||||
}
|
||||
|
||||
|
@ -280,6 +375,10 @@ void HttpClientTest::onHttpRequestCompleted(HttpClient *sender, HttpResponse *re
|
|||
printf("%c", (*buffer)[i]);
|
||||
}
|
||||
printf("\n");
|
||||
if (response->getHttpRequest()->getReferenceCount() != 2)
|
||||
{
|
||||
log("request ref count not 2, is %d", response->getHttpRequest()->getReferenceCount());
|
||||
}
|
||||
}
|
||||
|
||||
void HttpClientTest::toExtensionsMainLayer(cocos2d::Ref *sender)
|
||||
|
|
|
@ -13,11 +13,11 @@ public:
|
|||
void toExtensionsMainLayer(cocos2d::Ref *sender);
|
||||
|
||||
//Menu Callbacks
|
||||
void onMenuGetTestClicked(cocos2d::Ref *sender);
|
||||
void onMenuPostTestClicked(cocos2d::Ref *sender);
|
||||
void onMenuPostBinaryTestClicked(cocos2d::Ref *sender);
|
||||
void onMenuPutTestClicked(cocos2d::Ref *sender);
|
||||
void onMenuDeleteTestClicked(cocos2d::Ref *sender);
|
||||
void onMenuGetTestClicked(cocos2d::Ref *sender, bool isImmediate);
|
||||
void onMenuPostTestClicked(cocos2d::Ref *sender, bool isImmediate);
|
||||
void onMenuPostBinaryTestClicked(cocos2d::Ref *sender, bool isImmediate);
|
||||
void onMenuPutTestClicked(cocos2d::Ref *sender, bool isImmediate);
|
||||
void onMenuDeleteTestClicked(cocos2d::Ref *sender, bool isImmediate);
|
||||
|
||||
//Http Response Callback
|
||||
void onHttpRequestCompleted(cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response);
|
||||
|
|
Loading…
Reference in New Issue