Chat Server-decryption of strangers (9) chat message forwarding, chat server strangers

Source: Internet
Author: User

Chat Server-decryption of strangers (9) chat message forwarding, chat server 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.
Engineering: https://github.com/LineChen/

V. forwarding information
Because the client and server maintain a persistent connection, you can obtain the user's session according to the user ID, and receive the session to send messages.
For chat, it is mainly divided into online chat and offline chat. If both are online, you can directly forward the messages. When one party is offline, you need to save offline messages in the database and send them to the ta when the offline friends log on. To save offline messages, you must handle the messages (text messages, voice messages, and image messages) based on the message type ).
/**
* Send information to a single user
*
* @ Param moMoMsg
* @ Param getterId
*/
Private void sendMsgToUser (iMoMoMsg momomomsg, String getterId ){
If (ManageClientSession. isContainsId (getterId )){
ManageClientSession. getSession (getterId). write (moMoMsg );
// System. out. println ("forwarding successful ..");
} Else {
SqlModel model = new SqlModel ();
If (! Model. isTableExists ("mc _" + getterId) // "mc _" + userId
Model. createCacheTable (getterId); // create a cache Database
MsgDb msgDb = MsgTranceUtil. getInstance (). Trance_Net2Db (moMoMsg );
If (model. insertCacheMsg (msgDb, getterId )){
// System. out. println ("cache successful ");
} Else {
// System. out. println ("cache failed ");
}
}
}

The following are offline messages stored in the database and conversion types of messages sent:
Package com. imomo_msg;

Import java. text. SimpleDateFormat;
Import java. util. Date;

Import com. alibaba. fastjson. JSON;
Import com. alibaba. fastjson. JSONObject;
Import com. server_utils.FileTools;
Import com. server_utils.StaticValues;

/**
* Message package Conversion Tool
*
* @ Author Administrator
*
*/
Public class MsgTranceUtil {

Public static MsgTranceUtil getInstance () {return new MsgTranceUtil ();} /*** data inventory messages are converted to network transmission messages ** @ param msgDb * @ return */public iMoMoMsg Trance_Db2Net (MsgDb msgDb) {iMoMoMsg moMsg = new iMoMoMsg (); switch (msgDb. msgType) {case iMoMoMsgTypes. CHATING_TEXT_MSG: moMsg. symbol = '+'; moMsg. msgJson = msgDb. msgJson; break; case iMoMoMsgTypes. CHATING_IMAGE_MSG: JSONObject json = JSON. parseObject (msgDb. msgJson); moMsg. symbol = '-'; String imagePath = json. getString (MsgKeys. imagePath); moMsg. msgBytes = FileTools. getInstance (). getMultyFileBytes (imagePath); json. remove (MsgKeys. imagePath); moMsg. msgJson = json. toJSONString (); // Delete the local cached image FileTools. getInstance (). deleteFile (imagePath); break; case imomsgtypes. CHATING_VOICE_MSG: moMsg. symbol = '-'; JSONObject json2 = JSON. parseObject (msgDb. msgJson); String voicePath = json2.getString (MsgKeys. voicePath); moMsg. msgBytes = FileTools. getInstance (). getMultyFileBytes (voicePath); json2.remove (MsgKeys. voicePath); moMsg. msgJson = json2.toJSONString (); FileTools. getInstance (). deleteFile (voicePath); break; case imomsgtypes. ADD_FRIEND: moMsg. symbol = '+'; moMsg. msgJson = msgDb. msgJson; break; case iMoMoMsgTypes. RESET_HEAD: JSONObject resetHeadjson = JSON. parseObject (msgDb. msgJson); moMsg. symbol = '-'; String headPath = resetHeadjson. getString (MsgKeys. imagePath); moMsg. msgBytes = FileTools. getInstance (). getMultyFileBytes (headPath); resetHeadjson. remove (MsgKeys. imagePath); moMsg. msgJson = resetHeadjson. toJSONString (); // Delete the local cached image FileTools. getInstance (). deleteFile (headPath); break;} return moMsg;}/*** the network message is converted to the database storage message ** @ param moMsg * @ return */public MsgDb Trance_Net2Db (iMoMoMsg moMsg) {MsgDb msgDb = new MsgDb (); JSONObject json = JSON. parseObject (moMsg. msgJson); int msgtype = json. getIntValue (MsgKeys. msgType); switch (msgtype) {case imomsgtypes. CHATING_TEXT_MSG: break; case imomsgtypes. CHATING_IMAGE_MSG: String imagePath = StaticValues. MSG_CACHE_IMA_P_PATH + json. getString (MsgKeys. userId) + "_" + System. currentTimeMillis () + ". png "; FileTools. getInstance (). saveMultyFile (imagePath, moMsg. msgBytes); json. put (MsgKeys. imagePath, imagePath); break; case imomsgtypes. CHATING_VOICE_MSG: String voicePath = StaticValues. MSG_CACHE_VOI_P_PATH + json. getString (MsgKeys. userId) + "_" + System. currentTimeMillis () + ". amr "; FileTools. getInstance (). saveMultyFile (voicePath, moMsg. msgBytes); json. put (MsgKeys. voicePath, voicePath); break; case iMoMoMsgTypes. ADD_FRIEND:

// String headPath = StaticValues. HEAD_P_PATH
// + Json. getString (MsgKeys. userId) + pai.png ";
// Json. put (MsgKeys. imagePath, headPath );
Break;

    case iMoMoMsgTypes.RESET_HEAD:        String headPath = StaticValues.MSG_CACHE_IMA_P_PATH        + json.getString(MsgKeys.userId) + "_"        + System.currentTimeMillis() + ".png";        FileTools.getInstance().saveMultyFile(headPath, moMsg.msgBytes);        json.put(MsgKeys.imagePath, headPath);        break;    }    msgDb.msgType = msgtype;    msgDb.msgJson = json.toJSONString();    return msgDb;}

}

Then forward the offline message:
/**
* Forward offline messages
*
* @ Param session
* @ Param userId
* @ Param model
*/
Private void sendCacheMsg (IoSession session, String userId, SqlModel model ){
If (model. isTableExists ("mc _" + userId )){
If (model. getMsgCount (userId)> 0 ){
// Indicates that there is an offline message.
List list = model. getCacheMsgs (userId );
For (MsgDb msgDb: list ){
IMoMoMsg moMsg = MsgTranceUtil. getInstance (). Trance_Db2Net (
MsgDb );

Session. write (moMsg) ;}// clear the offline database file model. clearMsgCache (userId );}}}

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.