Decryption of strangers (4) -- Construction of message packets between clients and servers, construction of strangers

Source: Internet
Author: User

Decryption of strangers (4) -- Construction of message packets between clients and servers, construction of strangers

Tip: because the project is a little too big for me personally, it may be unclear in some aspects or the logic is not strong enough. If you have any questions, please @ me in time.

To send a custom message package, you must design the message structure between the client and the server.
Package com. imomo_msg;
/**
* Symbol = '+': this file is pure.
* Symbol = '-': Non-pure File
* @ Author Administrator
*/
Public class iMoMoMsg {
/* Symbol = '+': this file is pure. symbol = '-': this file is not pure./
Public char symbol; // determines whether it is a plain text file.
/* Explanation containing non-text details/
Public String msgJson;
/* Non-Text Information/
Public byte [] msgBytes;
@ Override
Public String toString (){
Return "iMoMoMsg [symbol =" + symbol + ", msgJson =" + msgJson + "]";
}
}

This is the structure of message packets for all communications. The symbol is identified by the codecs, and different encodings are implemented based on whether to send text messages or non-text messages. MsgJson is a JSON string of all text messages. For text messages, msgBytes = null. For non-text messages, msgBytes is a byte array of non-text messages, such as voice messages or images. MsgJson is not empty at this time. It contains the explanation of the voice or image. If the message is sent by someone, for example, the registration message must contain the user id and other information.

For text messages, a JSON string is sent here. Because JSON is better processed, it is very convenient to call Alibaba's open-source project fastjson. It should be noted that the json string must contain the Message Type key-value pair. here we need to design the message type according to actual needs. As follows:
Package com. imomo_msg;
Public interface iMoMoMsgTypes {
Int REGISTER = 0; // REGISTER
Int REGISTER_SUCCESS = 1; // registration successful
Int REGISTER_FAILED = 2; // registration failed
Int LOGIN = 3; // log on
Int LOGIN_SUCCESS = 4; // login successful
Int LOGIN_FAILED = 5; // Logon Failed
Int LOGIN_SUPER_HEAD = 6; // super login. the user's mobile phone has a user image and does not need to get an avatar.
Int LOGIN_SUPER_NOHEAD = 7; // super login. the user's mobile phone has no user image and needs to obtain profile pictures from the server.
Int LOGIN_SUPER_SUCCESS = 8; // success
Int LOGIN_SUPER_FAILED = 9; // failed
Int LOGOUT = 10; // offline
Int FIND_PASSWD = 11; // retrieve the password
Int FIND_PASSWD_SUCCESS = 12; // The password retrieval email has been sent.
Int FIND_PASSWD_FAILED = 13; // password retrieval failed
Int RESET_PASSWD = 14; // reset the password
Int RESET_PASSWD_SUCCESS = 15; // The password is successfully reset.
Int RESET_PASSWD_FAILED = 16; // Password Reset failed
Int RESET_USERNAME = 17; // modify the user name
// Int RESET_USERNAME_SUCCESS = 18; // the user name is successfully modified.
// Int RESET_USERNAME_FAILED = 19; // An error occurred while modifying the user name.
Int RESET_SEX = 200; // modify gender
Int RESET_BIRTHDAY = 201; // modify the birthday.
Int RESET_SIGNATUE = 202; // modify the personal signature
Int RESET_HEAD = 20; // modify the Avatar
Int RESET_HEAD_SUCCESS = 21; // the Avatar is successfully modified.
Int RESET_HEAD_FAILED = 22; // An error occurred while modifying the Avatar.
Int CHATING_TEXT_MSG = 23; // chat information-Text Information
Int CHATING_VOICE_MSG = 24; // chat information-voice information
Int CHATING_IMAGE_MSG = 25; // chat information-Image Information
Int CONNECT_DOWN = 26; // disconnect from the server
Int LOCATION = 27; // geographical LOCATION of the user
Int GET_STRANGERS_LOC_ONEKM = 28; // request to obtain the geographic location of a stranger around one kilometer
Int GET_STRANGERS_LOC_MORE = 40; // a person greater than one kilometer
Int STRANGERS_LIST_ONEKM = 31; // list of strangers within one kilometer
Int STRANGERS_LIST_MORE = 38; // more people nearby
Int NO_STRANGERS = 32; // No stranger around
Int GET_STRANGER_INFO = 50; // obtain the personal information of a stranger.
Int ADD_FRIEND = 33; // Add a friend
Int ADD_FRIEND_SUCCESS = 36; // added successfully
Int ADD_FRIEND_FAILED = 37; // added successfully
Int DELETE_FRIEND = 35; // deletes a friend.
Int GET_FRIEND_ID_LIST = 34; // obtain the friend Id list
Int FRIEND_ID_LIST = 42; // friend Id list
Int GETA_FRIEND_INFO_HEAD = 29; // get the specific information of a stranger (there is an image locally, and the server does not need to send an avatar)
Int GETA_FRIEND_INFO_NOHEAD = 30; // get the specific information of a stranger (no local image, the server wants to send an avatar)
Int REBACK = 51; // user feedback
Int GROUP_INVITE = 52; // group chat invitation
Int SIGN = 55; // SIGN in every day
Int CREATE_GROUP = 56; // create a group
Int CREATE_GROUP_SUCCESS = 57;
Int CREATE_GROUP_FAILED = 58;
Int INVITE_TO_GROUP = 59; // invite others to chat
Int AGREEE_TO_GROUP = 60; // agree to join the group
Int GROUP_MSG = 61;
Int PASS_GAME = 62; // pass the dynamic value
}

You also need to design the messages to be sent, that is, the JSON key.
Package com. imomo_msg;
/**
* JSON data key
*
* @ Author Administrator
*
*/
Public interface MsgKeys {
String msgType = "msgType"; // Message Type
String userId = "userId"; // The sender Id
String friendId = "friendId"; // recipient Id
String friendName = "friendName"; // friend name
String sendTime = "sendTime"; // sending time
String msgCotent = "msgCotent"; // chat information-Text Information
String voiceTime = "voiceTime"; // chat information-length of voice information
String voicePath = "voicePath"; // chat information-voice file path
String imagePath = "imagePath"; // chat information-image path
String userEmail = "userEmail"; // the user's registered email address. Use the email address to log on.
String userName = "userName"; // User Name
String userSex = "userSex"; // user gender
String userBirthday = "userBirthday"; // user's birthday
String userPasswd = "userPasswd"; // logon Password
String personSignature = "personSignature"; // personalized Signature
String vitalityValue = "vitalityValue"; // dynamic value
String userHeadPath = "userHeadPath"; // path of the user avatar
String loc_province = "loc_province"; // Province
String loc_longpolling = "loc_longpolling"; // longitude
String loc_Latitude = "loc_Latitude"; // latitude
String distRange = "distRange"; // The number of kilometers
String strangerList = "strangerList"; // list of strangers
String friendIdList = "friendIdList"; // list of friend IDS
String groupId = "groupId"; // group Id
String groupName = "groupName"; // group name
String groupTopic = "groupTopic"; // group topic
String groupCreator = "groupCreator ";//
String isGroupMsg = "isGroupMsg ";
}

Then we can construct a message:
// Test Registration
IMoMoMsg moMoMsg = new iMoMoMsg ();
JSONObject Json = new JSONObject ();
Json. put (MsgKeys. msgType, imomsgtypes. REGISTER );
Json. put (MsgKeys. userEmail, "157 ***** @ 163.com ");
Json. put (MsgKeys. userName, "isRunning ");
Json. put (MsgKeys. userPasswd, "run_run ");
Json. put (MsgKeys. userBirthday,"**");
Json. put (MsgKeys. userSex, "male ");
MoMoMsg. msgJson = Json. toJSONString ();
MoMoMsg. msgBytes = FileTools. getInstance (). getMultyFileBytes ("E: \ iMoMoServer \ ClientsHead \ fff.png ");
MoMoMsg. symbol = '-';
This is the construction of a non-text message. The text message does not have a byte array.

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.