public void sendMsg() { if (inputBox.ForeColor != Color.Gray || inputBox.Text != "") { textMessage message = new textMessage(); message.content = inputBox.Text; message.target = (int)targetUserID; message.sendUser = publicClass.mainUser.id; message.sendTime = DateTime.Now; AddMessage(message); if (publicClass.myChat.ContainsKey(message.target)) { publicClass.myChat[message.target].addMessage(message); } else { singleChatSession chatSession = new singleChatSession(message.target); chatSession.addMessage(message); publicClass.myChat.Add(message.target, chatSession); } string sendMessage = JsonConvert.SerializeObject(new command() { data = message, type = 1, subType = "singleChatTextMessage", res = "" }); //序列化 publicClass.cSocket.sendSysMsg(sendMessage, 0); } }
public void DrawMessage() { if (!publicClass.myChat.ContainsKey((int)targetUserID)) { singleChatSession chatSession = new singleChatSession((int)targetUserID); publicClass.myChat.Add((int)targetUserID, chatSession); } ArrayList messages = publicClass.myChat[(int)targetUserID].messageList; singleChatMessageBox.Text = ""; foreach (baseMessage i in messages) { if (i.GetType() == typeof(textMessage)) { singleChatMessageBox.SelectionFont = new Font("黑体", 9, FontStyle.Bold); singleChatMessageBox.SelectionColor = System.Drawing.Color.Purple; singleChatMessageBox.SelectionIndent = 2; singleChatMessageBox.SelectionBullet = true; singleChatMessageBox.AppendText(publicClass.onlineUserList.Find(s => s.id == i.sendUser).userName + " [" + i.sendTime + "]\r\n"); singleChatMessageBox.SelectionFont = new Font("宋体", 9, FontStyle.Regular); singleChatMessageBox.SelectionColor = System.Drawing.Color.Black; singleChatMessageBox.SelectionIndent = 19; singleChatMessageBox.SelectionBullet = false; singleChatMessageBox.AppendText(((textMessage)i).content + "\r\n"); } else if (i.GetType() == typeof(imageFileMessage)) { singleChatMessageBox.SelectionFont = new Font("黑体", 9, FontStyle.Bold); singleChatMessageBox.SelectionColor = System.Drawing.Color.Purple; singleChatMessageBox.SelectionIndent = 2; singleChatMessageBox.SelectionBullet = true; singleChatMessageBox.AppendText(publicClass.onlineUserList.Find(s => s.id == i.sendUser).userName + " [" + i.sendTime + "]\r\n"); singleChatMessageBox.SelectionFont = new Font("宋体", 9, FontStyle.Regular); singleChatMessageBox.SelectionColor = System.Drawing.Color.Blue; singleChatMessageBox.SelectionIndent = 19; singleChatMessageBox.SelectionBullet = false; singleChatMessageBox.Text = singleChatMessageBox.Text + "\r\n" + "【图片/文件消息 点击查看】"; } } }
//单聊图片文件信息解析 public static void decodeSingleIFMessageDraw(command cComand) { JArray data = (JArray)cComand.data;//转化为Jobject int uID = data[1].ToObject <int>(); int targetID = data[2].ToObject <int>(); string fileName = data[3].ToString(); imageFileMessage message = new imageFileMessage(); message.fileName = fileName; try { string endfix = fileName.Substring(fileName.IndexOf('.') + 1); if (endfix == "jpg" | endfix == "jpeg" | endfix == "png" | endfix == "gif") { message.fileType = "img";//图片类型 } } catch { message.fileType = "file";//文件类型 } message.sendUser = uID; message.sendTime = DateTime.Now; if (!myChat.ContainsKey(message.sendUser)) { singleChatSession chatSession = new singleChatSession(message.sendUser); chatSession.addMessage(message); myChat.Add(message.sendUser, chatSession); if (s1 == null) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else if (s1.targetUserID != (uint)message.sendUser) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else if (s1.targetUserID == (uint)message.sendUser) { if (s1.IsDisposed) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else { s1.Invoke(new Action(() => { s1.AddMessage(message); })); } } } else { myChat[message.sendUser].addMessage(message); if (s1 == null) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else if (s1.targetUserID != (uint)message.sendUser) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else if (s1.targetUserID == (uint)message.sendUser) { if (s1.IsDisposed) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else { s1.Invoke(new Action(() => { s1.AddMessage(message); })); } } } }
//单聊文本信息解析 public static void decodeSingleTextMessageDraw(command cComand) { JObject data = (JObject)cComand.data; //转化为Jobject textMessage message = data.ToObject <textMessage>(); //获取消息类 if (!myChat.ContainsKey(message.sendUser)) { singleChatSession chatSession = new singleChatSession(message.sendUser); chatSession.addMessage(message); myChat.Add(message.sendUser, chatSession); if (s1 == null) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else if (s1.targetUserID != (uint)message.sendUser) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else if (s1.targetUserID == (uint)message.sendUser) { if (s1.IsDisposed) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else { s1.Invoke(new Action(() => { s1.AddMessage(message); })); } } } else { myChat[message.sendUser].addMessage(message); if (s1 == null) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else if (s1.targetUserID != (uint)message.sendUser) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else if (s1.targetUserID == (uint)message.sendUser) { if (s1.IsDisposed) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else { s1.Invoke(new Action(() => { s1.AddMessage(message); })); } } } }