void msgServer_Message(object sender, MsgServerEventArgs e) { IMLibrary3.Protocol.Message msg = e.obj as IMLibrary3.Protocol.Message; msg.DateTime = DateTime.Now.ToString(); //将消息发送时间设置为服务器的时间 if (msg.MessageType == IMLibrary3.Enmu.MessageType.User) //如果消息发送给用户 { SendMessageToUser(msg.to, msg); } else if (msg.MessageType == IMLibrary3.Enmu.MessageType.Group)//如果消息发送给群 { SendMessageToRoom(msg.from, msg.to, msg); } else if (msg.MessageType == IMLibrary3.Enmu.MessageType.Notice) //如果发送通知消息给多个用户 { string[] users = msg.to.Split(';'); //获得要接收消息的用户数据 if (users.Length > 0) { foreach (string userID in users) { msg.to = userID; SendMessageToUser(userID, msg); } } } else if (msg.MessageType == IMLibrary3.Enmu.MessageType.broadcasting)//如果发送通知消息给所有用户 { BroadcastingMessageToOnlineUser(msg); } }
/// <summary> /// 添加离线消息到数据库 /// </summary> /// <param name="msg"></param> public static void addMessageToDB(IMLibrary3.Protocol.Message msg) { string sql = "insert into RecordMsg(userID,MessageType,froms,tos,datetime,MessageXML) values(@userID,@MessageType,@froms,@tos,@datetime,@MessageXML)"; System.Data.SqlClient.SqlParameter[] sqlpar = new System.Data.SqlClient.SqlParameter[] { new System.Data.SqlClient.SqlParameter("@userID", msg.to), new System.Data.SqlClient.SqlParameter("@MessageType", msg.MessageType), new System.Data.SqlClient.SqlParameter("@froms", msg.from), new System.Data.SqlClient.SqlParameter("@tos", msg.to), new System.Data.SqlClient.SqlParameter("@datetime", DateTime.Now.ToString()), new System.Data.SqlClient.SqlParameter("@MessageXML", IMLibrary3.Protocol.Factory.CreateXMLMsg(msg)), }; IMLibrary.SqlData.DataAccess.ExecSql(sql, sqlpar); }
/// <summary> /// 添加消息 /// </summary> public static void AddMsg(IMLibrary3.Protocol.Message Msg) { string sql = "insert into MsgRecord(froms,tos,MessageType,DateTime,Vcard)" + "values(@froms,@tos,@MessageType,@DateTime,@Vcard)"; System.Data.SQLite.SQLiteParameter[] parameters = new System.Data.SQLite.SQLiteParameter[] { new System.Data.SQLite.SQLiteParameter("@froms", Msg.from), new System.Data.SQLite.SQLiteParameter("@tos", Msg.to), new System.Data.SQLite.SQLiteParameter("@MessageType", Convert.ToByte(Msg.MessageType)), new System.Data.SQLite.SQLiteParameter("@DateTime", Msg.DateTime == null?DateTime.Now.ToString():Msg.DateTime), new System.Data.SQLite.SQLiteParameter("@Vcard", IMLibrary3.Protocol.Factory.CreateXMLMsg(Msg)) }; try { SQLiteDBHelper.ExecuteNonQuery(sql, parameters); } catch (Exception ex) { MessageBox.Show(ex.Source + ex.Message); } }
/// <summary> /// 发送消息到群 /// </summary> /// <param name="userID"></param> /// <param name="roomID"></param> /// <param name="msg"></param> public void SendMessageToRoom(string userID, string roomID, Message msg) { string xmlmsg = Factory.CreateXMLMsg(msg); SendMessageToRoom(userID, roomID, xmlmsg); }
/// <summary> /// 获得 DR 消息 /// </summary> /// <param name="dr"></param> /// <returns></returns> public static IMLibrary3.Protocol.Message GetDrMsg(System.Data.SQLite.SQLiteDataReader dr) { IMLibrary3.Protocol.Message msg = IMLibrary3.Protocol.Factory.CreateInstanceObject(dr["Vcard"].ToString()) as IMLibrary3.Protocol.Message; return(msg); }
/// <summary> /// 处理用户请求转发聊天消息 /// </summary> /// <param name="presence"></param> /// <param name="session"></param> private void onMessage(Message msg, TCPServerSession session) { msg.DateTime = DateTime.Now.ToString();//将消息发送时间设置为服务器的时间 if (msg.MessageType == IMLibrary3.Enmu.MessageType.User)//如果消息发送给用户 { SendMessageToUser(msg.to, msg); } else if (msg.MessageType == IMLibrary3.Enmu.MessageType.Group) { SendMessageToRoom(msg.from, msg.to, msg); } else if (msg.MessageType == IMLibrary3.Enmu.MessageType.Notice)//如果发送通知消息给多个用户 { string[] users = msg.to.Split(';');//获得要接收消息的用户数据 if (users.Length > 0) foreach (string userID in users) { msg.to = userID; SendMessageToUser(userID, msg); } } else if (msg.MessageType == IMLibrary3.Enmu.MessageType.broadcasting)//如果发送通知消息给所有用户 BroadcastingMessageToOnlineUser(msg); }