I have the following code:
void GameScene::onGetServersRequestCompleted( cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response )
{
if( 200 == response->getResponseCode() ) {
std::vector<char> *buffer = response->getResponseData();
std::string serversString( buffer->begin(), buffer->end() );
Json::Reader reader;
Json::Value root;
reader.parse(serversString, root);
const Json::Value servers = root["servers"];
for ( int i = 0; i < servers.size(); ++i ) {
//CCLOG("%s", servers[i].asString().c_str());
cocos2d::network::HttpRequest *request = new cocos2d::network::HttpRequest();
request->setUrl("http://" + servers[i].asString() + "/info");
request->setRequestType(cocos2d::network::HttpRequest::Type::GET);
request->setResponseCallback(CC_CALLBACK_2(GameScene::onServerRequestCompleted, this));
cocos2d::network::HttpClient::getInstance()->send(request);
request->release();
}
} else {
CCLOG("Request Failed! Response Code: %i\n", response->getResponseCode());
/*std::vector<char> *buffer = response->getResponseData();
std::string serversString( buffer->begin(), buffer->end() );
CCLOG("%s", serversString.c_str());*/
this->serverIp = "185.8.166.41";
}
}
I am trying to get the right server. What I would like to do is to get the execution time of each server's request and get the fastest one. Is it possible? because it's an asynchronous function I don't know how to do it. How would you do it?
Does this help? From the documentation of HTTPRequest and HTTPResponse ....
You can set a string tag to identify your request, this tag can be found in HttpResponse->getHttpRequest->getTag()
void HTTPRequest::setTag(const char *tag);
Get the string tag back to identify the request. The best practice is to use it in your MyClass::onMyHttpRequestCompleted(sender, HttpResponse*) callback
const char* HTTPResponse::getTag();
Edit: You can use these unique tags to pair up the requests with asynchronous responses in a global hash and thus measure "response time". There doesn't seem to be any interface in HTTP* classes to give you the time of execution.
Edit: There are two ways to pair up HTTPResponse with a HTTPRequest. Here I try to use the second method with some quick/dirty code. Please convert/use it as you see fit.
Create a file HTTPCustomData.h :
#include <time.h>
class HTTPCustomData {
private:
time_t t1;
public:
HTTPCustomData() { time(&t1); }
double getTimePassed(void) {
time_t t2; time(&t2); return(difftime(t2,t1));
}
};
Use the following code where you send request:
#include "HTTPCustomData.h"
for ( int i = 0; i < servers.size(); ++i ) {
// ...
// ... Before "send"
request->setUserData(new HTTPCustomData());
cocos2d::network::HttpClient::getInstance()->send(request);
request->release();
}
Later in GameScene::onServerRequestCompleted method, ...
#include <memory>
#include <iostream>
#include "HTTPCustomData.h"
void GameScene::onServerRequestCompleted(...) {
{
// ...
std::unique_ptr<HTTPCustomData> h(
(HTTPCustomData *)(response->getHTTPRequst()->getUserData())
);
std::cout << h->getTimePassed() << std::endl;
// ....
}