A basic process
1. Assemble 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; // command no. 1001
Root ["opIndex"] = 0;
String info = root. toStyledString ();
2. send commands
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); // send command
NetWaitManager: getInstance ()-> checkNetWait (code); // Based on the sending command, determine whether there is a reply command to wait for. If yes, add it to the waiting list.
}
3. receive commands through real-time monitoring
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. Resolution command received
SyncDispatch (code, json );
NetWaitManager: getInstance ()-> removeWaitCode (code); // remove the command from the waiting list
}
MNetDataList. clear ();
}
}
Void NetDataReceive: syncDispatch (int32_t code, string json)
{
Json: Reader reader;
Json: Value root;
If (! Reader. parse (json, root) {// parsing failed
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:
{
// Parse 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 user data
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 );
}
}