脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-coocs2d-x + scoket 解析Json 流程总结,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。
一个基本流程
1.组装json
void NetDataSend::sendChat(char tyPE, int playerId, int ITemIndex, string infostr){
Json::Value root;
root["type"] = type;
root["playerId"] = playerId;
root["itemIndex"] = itemIndex;
root["info"] = infoStr;
root["index"] = C_CHAT;//指令号1001
root["opIndex"] = 0;
string info =root.toStyledString();
2.发送指令
writeCmd(C_CHAT,info);
}
void NetDataSend::writeCmd(int code, string info){
const char *data=info.c_str();
int size=info.size();
CGameNetBean::sharedNetBean()->writeCmd(code,data,size);//发送指令
NetWaitManager::getInstance()->checkNetWait(code);//根据发送指令,判断是否有需要等待的回复指令,有则添加到等待列表中
}
3.实时监测接受指令
void NetDataReceive::updateNetDate()
{
if(mNetDataList.size()>0){
for(unsigned int i=0;i
string data=mNetDataList[i];
int index=data.find("|");
int code=getCode(data,index);
string json=getJson(data, index);
4.接收到就解析指令
syncDispatch(code,json);
NetWaitManager::getInstance()->removeWaitCode(code);//移除等待列表中的指令
}
mNetDataList.clear();
}
}
void NetDataReceive::syncDispatch(int32_t code, string json)
{
Json::Reader reader;
Json::Value root;
if(!reader.parse(json, root)){//解析失败
if (strlen(json.c_str())>10*1024)
{
CCLOG("===========>parse JSON error jsonstr: %s",json.c_str());
}else{
CCLOG("===========>parse json error code: %i",code);
}
return;
}
switch (code){
case S_CHAT:
{
//解析json
this->toChat(root);
}
break;
}
}
void NetDataReceive::toChat( Json::Value root ){
S_Chat* sChat = new S_Chat();
sChat->type=root["type"].asInt();
sChat->sayPlayerId=root["sayPlayerId"].asInt();
sChat->sayRoleName=root["sayRoleName"].asString();
sChat->saySex=root["saySex"].asInt();
sChat->receiverPlayerId=root["receiverPlayerId"].asInt();
sChat->receiverRoleName=root["receiverRoleName"].asString();
sChat->info=root["info"].asString();
sChat->vipLv=root["vipLv"].asInt();
//UI界面使用数据
mChatInfo = new ChatInfo();
mChatInfo->info = m_chat->info;
// mChatInfo->itemBean = m_chat->itemBean;
mChatInfo->receiverPlayerId = m_chat->receiverPlayerId;
mChatInfo->receiverRoleName = m_chat->receiverRoleName;
mChatInfo->sayPlayerId = m_chat->sayPlayerId;
mChatInfo->sayRoleName = m_chat->sayRoleName;
mChatInfo->saySex = m_chat->saySex;
mChatInfo->type = m_chat->type;
mChatInfo->vip = m_chat->vipLv;
CCLOG("mChatInfo->vip == > %d",mChatInfo->vip);
ChatBoardUI* mChatBoardUI=ChatBoardUI::getInstance();
if (mChatBoardUI) {
mChatBoardUI->updateUI(mChatInfo);
}
}
觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!
以上是脚本宝典为你收集整理的javascript代码实例教程-coocs2d-x + scoket 解析Json 流程总结全部内容,希望文章能够帮你解决javascript代码实例教程-coocs2d-x + scoket 解析Json 流程总结所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。