cocos2dx 获取网络时间

来源:互联网 发布:nginx处理空header 编辑:程序博客网 时间:2024/06/10 14:28

一般消除游戏都有体力回复的功能,很多消除游戏都是取的本地时间,这样的坏处就是用户修改了手机的时间就可以回复满体力。

实现代码如下 :

思路就是使用百度的时间api获取到网页,然后在网页中抓取到时间戳 查看网页的源代码就可以发现源代码中有一个

window.baidu_time(1447841230045); 这一串数字就是时间戳 这里我们只要精确到秒级所以取前面10位就可以。

抓取时间戳就是通过window.baidu_time(关键字把网页源代码字符串切割成两半 然后取后面一半的前面10个字符,再把字符串转换为时间就可以了。

#ifndef __NETTIME__H#define __NETTIME__H#include "cocos2d.h";#include "network/HttpClient.h"using namespace cocos2d;using namespace network;class NetTime : public Node{public:NetTime();~NetTime();CREATE_FUNC(NetTime);CC_SYNTHESIZE(int, m_day,Day);CC_SYNTHESIZE(int, m_moth, Month);CC_SYNTHESIZE(int, m_year, Year);CC_SYNTHESIZE(int, m_hour, Hour);CC_SYNTHESIZE(int, m_second, Second);CC_SYNTHESIZE(int, m_minute, Minute);CC_SYNTHESIZE(time_t, m_time, Time);//时间chuovoid requestNetTime();private:void onHttpComplete(HttpClient * sender, HttpResponse * response);void spliteTime(std::string htmlStr);void initTime(time_t time);};#endif

#include "NetTime.h"#include "Utils.h"NetTime::NetTime() :m_second(0), m_day(0), m_hour(0), m_year(0), m_minute(0), m_moth(0){}NetTime::~NetTime(){}void NetTime::requestNetTime(){HttpRequest * request = new HttpRequest();request->setUrl("http://open.baidu.com/special/time/"); //百度获取时间的apirequest->setRequestType(HttpRequest::Type::GET);request->setTag("WebTime");request->setResponseCallback(this, httpresponse_selector(NetTime::onHttpComplete));HttpClient::getInstance()->send(request);}void NetTime::onHttpComplete(HttpClient * sender, HttpResponse * response){if (!response)return;if (0 != strlen(response->getHttpRequest()->getTag())){CCLOG("%s completed", response->getHttpRequest()->getTag());}int statusCode = response->getResponseCode();char statusString[64] = {};sprintf(statusString, "HTTP Status: %d, tag = %s", statusCode, response->getHttpRequest()->getTag());CCLOG("%s", statusString);if (!response->isSucceed()){CCLOG("response failed");CCLOG("error buffer:%s", response->getErrorBuffer());return;}std::vector<char> * buffer = response->getResponseData();std::string str;for (unsigned i = 0; i < buffer->size(); i++){char a = (*buffer)[i];str.append(1, a);}spliteTime(str);//readJson(str);}void NetTime::spliteTime(std::string htmlStr){std::vector<std::string> tmp;splitString(tmp, htmlStr, "baidu_time(");std::string str = tmp.at(1);std::string tmpTime = str.substr(0,10) ;time_t tt = atoi(tmpTime.c_str());  initTime(tt);}void NetTime::initTime(time_t time){time_t t;struct tm *p;t = time;p = localtime(&t);/*char s[100];strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", p);printf("%d: %s\n", (int)t, s);*/setSecond(p->tm_sec);setDay(p->tm_mday);setMonth(p->tm_mon + 1);setYear(p->tm_year + 1900 );setMinute(p->tm_min);setHour(p->tm_hour + 8);//北京时间等于世界时间加8小时setTime(t);}


切割字符串函数

//切割字符串static void splitString(std::vector<std::string> &contentVector,std::string content, std::string pattern){std::string::size_type pos;//std::vector<std::string> result;  content += pattern;//扩展字符串以方便操作  int size = content.size();for (int i = 0; i<size; i++){pos = content.find(pattern, i);if (pos<size){std::string s = content.substr(i, pos - i);// result.push_back(s);  contentVector.push_back(s);i = pos + pattern.size() - 1;}}}



0 0
原创粉丝点击