/// <summary> /// 发送消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void sendMsg_Click(object sender, RoutedEventArgs e) { onlineFriendType sendTo = (onlineFriendType)onlineFriendlistListBox.SelectedItem; byte[] arrMsg, sendArrMsg; Paragraph newParagraph = new Paragraph(); Run run = new Run() { Text = "我 " + System.DateTime.Now.ToString() + "\r\n", Foreground = new SolidColorBrush(Colors.Blue) }; newParagraph.Inlines.Add(run); InlineUIContainer inlineUIContainer = new InlineUIContainer() { Child = new TextBlock() { Foreground = new SolidColorBrush(Colors.Black), TextWrapping = TextWrapping.Wrap, Text = " " + inputTextBox.Text + "\r\n" } }; newParagraph.Inlines.Add(inlineUIContainer); if (sendTo.nameAndId.Equals("所有人")) { MsgHandler msgHandler = new MsgHandler(userData.userId, userData.userName, "", inputTextBox.Text); string sendMsg = JsonConvert.SerializeObject(msgHandler); arrMsg = Encoding.UTF8.GetBytes(sendMsg); sendArrMsg = new byte[arrMsg.Length + 1]; // 设置标志位,代表更新用户信息 sendArrMsg[0] = MSG_TO_ALL; Buffer.BlockCopy(arrMsg, 0, sendArrMsg, 1, arrMsg.Length); dictParagraph["AllUsers"].Add(newParagraph); chatRichTextBox.Document.Blocks.Add(newParagraph); } else { MsgHandler msgHandler = new MsgHandler(userData.userId, userData.userName, sendTo.userData.userId, inputTextBox.Text); string sendMsg = JsonConvert.SerializeObject(msgHandler); arrMsg = Encoding.UTF8.GetBytes(sendMsg); sendArrMsg = new byte[arrMsg.Length + 1]; // 设置标志位,代表更新用户信息 sendArrMsg[0] = MSG_TO_INDIVIDULAL; Buffer.BlockCopy(arrMsg, 0, sendArrMsg, 1, arrMsg.Length); dictParagraph[sendTo.userData.userId].Add(newParagraph); chatRichTextBox.Document.Blocks.Add(newParagraph); } try { socketClient.Send(sendArrMsg); inputTextBox.Text = ""; } catch (SocketException se) { Console.WriteLine("【错误】发送消息异常:" + se.Message); return; } catch (Exception ex) { Console.WriteLine("【错误】发送消息异常:" + ex.Message); return; } }
/// <summary> /// 接收到群发给所有人的消息 /// </summary> /// <param name="msgHandler"></param> private void ReceiveMsgForIndividual(MsgHandler msgHandler) { List<Paragraph> userParagraphList = dictParagraph[msgHandler.from]; Paragraph newParagraph = new Paragraph(); Run run = new Run() { Text = msgHandler.fromName + " " + System.DateTime.Now.ToString() + "\r\n", Foreground = new SolidColorBrush(Colors.Red) }; newParagraph.Inlines.Add(run); InlineUIContainer inlineUIContainer = new InlineUIContainer() { Child = new TextBlock() { Foreground = new SolidColorBrush(Colors.Black), TextWrapping = TextWrapping.Wrap, Text = " " + msgHandler.message + "\r\n" } }; newParagraph.Inlines.Add(inlineUIContainer); userParagraphList.Add(newParagraph); onlineFriendType sendTo = (onlineFriendType)onlineFriendlistListBox.SelectedItem; if (sendTo.nameAndId.Equals("所有人") == false && sendTo.userData.userId.Equals(msgHandler.from)) chatRichTextBox.Document.Blocks.Add(newParagraph); else { int index; if (FindItemByUserId(msgHandler.from, out index)) { onlineFriendType user = (onlineFriendType)onlineFriendlistListBox.Items[index]; user.increaseNum(); IntPtr activeForm = GetActiveWindow(); // 先得到当前的活动窗体 Prompt test = new Prompt("来自 " + msgHandler.fromName + "(" + msgHandler.from + ") 的新消息", 0); cnt = (cnt + 1) % 6; test.Show(); SetActiveWindow(activeForm); // 在把焦点还给之前的活动窗体 } } }