Summary of coocs2d-x + scoket parsing Json Process

Source: Internet
Author: User

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 );

}



}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.