Tip: Because the project is a little bit large for me personally, so may be in some aspects of unclear or logic is not strong enough, if there is a problem please @ me.
Original project: https://github.com/LineChen/
v. Forwarding information
because the client and the server are long connected, you can get the user's session sessions based on the user ID, and the message can be sent by the session.
for chat, the main sub-chat between both online and one side offline situation. All the online situation is good, direct forwarding; when one side is offline, it is necessary to save the offline message in the database and send it to TA when the offline friend logs in. Saving offline messages you need to be aware of the corresponding processing (text messages, voice messages, picture messages) based on the message type.
/**
* Send information to a single user
*
* @param momomsg
* @param getterid
*/
private void Sendmsgtouser (Imomo MSG momomsg, String getterid) {
if (Manageclientsession.iscontainsid (Getterid)) {
Manageclientsession.getsession (Getterid). write (momomsg);
//SYSTEM.OUT.PRINTLN ("forwarded successfully:");
} else {
Sqlmodel model = new Sqlmodel ();
if (!model.istableexists ("MC_" + Getterid)//"MC_" + userId
model.createcachetable (Getterid);//Create cache database
Ms GDb msgdb = Msgtranceutil.getinstance (). TRANCE_NET2DB (MOMOMSG);
if (model.insertcachemsg (Msgdb, Getterid)) {
//System.out.println ("Cache succeeded");
} else {
//System.out.println ("Cache failed");
}
}
}
below are the offline messages stored in the database and the message conversion classes that are 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 Packet Conversion tool
*
* @author Administrator
*
*/
public class Msgtranceutil {
public static Msgtranceutil getinstance () {return new Msgtranceutil ();} /** * Database memory message converted to network transmission message * * @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 local cache picture Filetools.getinstance (). DeleteFile (ImagePath); Break Case iMoMoMsgTypes.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 IMoMoMsgTypes.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 local cache picture Filetools.getinstance (). DeleteFile (Headpath); Break } return momsg;} /** * Network messages into database store messages * * @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 iMoMoMsgTypes.CHATING_TEXT_MSG:break; Case IMoMoMsgTypes.CHATING_IMAGE_MSG:String ImagePath = Staticvalues.msg_cache_ima_p_path + json.ge Tstring (Msgkeys.userid) + "_" + system.currenttimemillis () + ". png"; Filetools.getinstance (). Savemultyfile (ImagePath, momsg.msgbytes); Json.put (Msgkeys.imagepath, ImagePath); Break Case IMoMoMsgTypes.CHATING_VOICE_MSG:String Voicepath = Staticvalues.msg_cache_voi_p_path + json.ge Tstring (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) + ". 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) {
Description has offline message
List List = Model.getcachemsgs (userId);
for (Msgdb msgdb:list) {
Imomomsg momsg = Msgtranceutil.getinstance (). Trance_db2net (
MSGDB);
session.write(moMsg); } // 清空数据库离线文件 model.clearMsgCache(userId); } }}
Chat Server-Decrypt Strangers (9) Chat message forwarding