霸气三国2先说下这么项目的进度 (进度有点慢 每天晚上写一点 努力坚持下去吧 )已经完成了消息系统 每个模块之间都是发送消息 来完成 数据通信的。
现在有 主场景模块、信息公告模块 、武将模块这三个模块 都是在主场景模块上来 撸的代码 流程如下:
进入主场景之前 会请求服务器发送scene.main请求
local pProxy = EMFacade:getInstance():retrieveProxy("SceneMainProxy")
if(pProxy~=nil) then
pProxy:getSceneMain()
end
function SceneMainProxy:getSceneMain()
self:sendRequest("","&login=1",PTLNM.GET_SCENEMAIN)
-- body
end
void EMHttpClient::sendRequest(string url,string method)
{
HttpRequest* request = new HttpRequest();
CCLOG("%s",url.c_str());
request->setUrl(url.c_str());
request->setRequestType(HttpRequest::Type::GET);
// write the post data
request->setResponseCallback(CC_CALLBACK_2(EMHttpClient::onHttpRequestCompleted, this));
request->setTag(method.c_str());
HttpClient::getInstance()->setTimeoutForConnect(1500);
HttpClient::getInstance()->sendImmediate(request);
HttpClient::getInstance()->enableCookies(NULL);
request->release();
}
void EMHttpClient::onHttpRequestCompleted(HttpClient *sender, HttpResponse *response)
{
if (!response)
{
return;
}
// You can get original request type from: response->request->reqType
if (0 != strlen(response->getHttpRequest()->getTag()))
{
log("%s completed", response->getHttpRequest()->getTag());
}
long statusCode = response->getResponseCode();
char statusString[64] = {};
sprintf(statusString, "HTTP Status Code: %ld, tag = %s", statusCode, response->getHttpRequest()->getTag());
log("response code: %ld", statusCode);
if (!response->isSucceed())
{
log("response failed");
log("error buffer: %s", response->getErrorBuffer());
EMFacade::getInstance()->sendNotification("OTHER_UNLOCK",0);
return;
}
// dump data
std::vector *m_responseData =response->getResponseData();
long data_length = m_responseData->size();
string json;
for (long i = 0;i<data_length;++i) { json+=m_responseData->at(i);
}
EMFacade::getInstance()->sendNotification(response->getHttpRequest()->getTag(),json);
}
这样 c++中就是接受这些数据进行请求了
当客户端接受到服务器传回的数据后 会发送一个response->getHttpRequest()->getTag() 的消息 这个消息是从request->setTag(method.c_str());中获取的 目的就是知道这个消息是什么类型的 请求的是什么类型 服务器发送回来的就是什么类型 这是统一的
将得到的数据 以json字符串的格式传到 控制层 。
if notification then
notification = tolua.cast(notification, "EMNotification");
local jsonData = notification:getJsonData()
local cjson = require "cjson"
local data = cjson.decode(jsonData)
if data.rc ~= 0 then
cclog("数据出错")
else
--初始化主场景
local sceneMainProxy = EMFacade:getInstance():retrieveProxy("SceneMainProxy")
if nil ~= sceneMainProxy then
sceneMainProxy.uid=data.user.uid;
sceneMainProxy.name=data.user.name;
end
--初始化武将
local generalProxy = EMFacade:getInstance():retrieveProxy("GeneralProxy")
if nil ~=generalProxy then
--武将信息
generalProxy.generals_info=data.data.generals.generals_info
--武将列表
generalProxy.gid_list=data.data.generals.gid_list
--武将布阵信息列表
generalProxy.formations_info=data.data.generals.formations_info
end
EMFacade:getInstance():sendNotification(QMSG.LOAD_COMPLETE,0)
end
end
获取的数据 才进行二次处理 就是我们客户端要用到的数据了 。其中客户端用到了 cjson
文件结构 如图:
这是消息系统中文件结构
记录下为了以后方便回顾。洗洗睡了。。