获取http请求的执行时间
Get execution time of http request
我有以下代码:
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";
}
}
我正在尝试获取合适的服务器。我想做的是获取每个服务器请求的执行时间并获得最快的一个。可能吗?因为它是一个异步函数,所以我不知道该怎么做。你会怎么做?
这有帮助吗?从文档
HTTPRequest and HTTPResponse ....
你可以设置一个字符串标签来标识你的请求,这个标签可以在HttpResponse->getHttpRequest->getTag()
void HTTPRequest::setTag(const char *tag);
取回字符串标记以识别请求。
最佳做法是在 MyClass::onMyHttpRequestCompleted(sender, HttpResponse*) 回调
中使用它
const char* HTTPResponse::getTag();
编辑:您可以使用这些独特的标签将请求与全局哈希中的异步响应配对,从而测量 "response time"。 HTTP* 类 好像没有什么接口给你执行时间。
编辑:
有两种方法可以将 HTTPResponse 与 HTTPRequest 配对。在这里,我尝试将第二种方法与一些 quick/dirty 代码一起使用。请convert/use你觉得合适。
创建文件 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));
}
};
在发送请求的地方使用以下代码:
#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();
}
稍后在 GameScene::onServerRequestCompleted 方法中,...
#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;
// ....
}
我有以下代码:
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";
}
}
我正在尝试获取合适的服务器。我想做的是获取每个服务器请求的执行时间并获得最快的一个。可能吗?因为它是一个异步函数,所以我不知道该怎么做。你会怎么做?
这有帮助吗?从文档 HTTPRequest and HTTPResponse ....
你可以设置一个字符串标签来标识你的请求,这个标签可以在HttpResponse->getHttpRequest->getTag()
void HTTPRequest::setTag(const char *tag);
取回字符串标记以识别请求。 最佳做法是在 MyClass::onMyHttpRequestCompleted(sender, HttpResponse*) 回调
中使用它 const char* HTTPResponse::getTag();
编辑:您可以使用这些独特的标签将请求与全局哈希中的异步响应配对,从而测量 "response time"。 HTTP* 类 好像没有什么接口给你执行时间。
编辑: 有两种方法可以将 HTTPResponse 与 HTTPRequest 配对。在这里,我尝试将第二种方法与一些 quick/dirty 代码一起使用。请convert/use你觉得合适。
创建文件 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));
}
};
在发送请求的地方使用以下代码:
#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();
}
稍后在 GameScene::onServerRequestCompleted 方法中,...
#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;
// ....
}