/// <summary> /// 新增客户端 /// </summary> /// <param name="conn"></param> public void AddClientListBoxItem(User conn) { if (clientListBox.InvokeRequired) { // 当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它 Action<User> actionDelegate = (x) => { this.clientListBox.Items.Add(new ItemKeyValue(x.name.ToString(), x)); }; // 或者 // Action<string> actionDelegate = delegate(string txt) { this.label2.Text = txt; }; this.msgHouse.Invoke(actionDelegate, conn); } else { this.clientListBox.Items.Add(new ItemKeyValue(conn.name.ToString(), conn)); } }
/// <summary> /// 删除客户端 /// </summary> /// <param name="toDeleteUser"></param> public void DelClientListBoxItem(User toDeleteUser) { if (clientListBox.InvokeRequired) { // 当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它 Action<User> actionDelegate = (x) => { ItemKeyValue tmp = null; foreach (ItemKeyValue item in this.clientListBox.Items) { if (((User)item.Value).id == x.id) { tmp = item; break; } } if (tmp != null) { this.clientListBox.Items.Remove(tmp); } }; // 或者 // Action<string> actionDelegate = delegate(string txt) { this.label2.Text = txt; }; this.msgHouse.Invoke(actionDelegate, toDeleteUser); } else { ItemKeyValue tmp = null; foreach (ItemKeyValue item in this.clientListBox.Items) { if (((User)item.Value).id == toDeleteUser.id) { tmp = item; break; } } if (tmp != null) { this.clientListBox.Items.Remove(tmp); } } }
/// <summary> /// 更新会话大厅消息 /// </summary> /// <param name="str"></param> public void AddMsgHouseNewMsg(User user, object msg) { if (msgHouse.InvokeRequired) { // 当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它 Action<object> actionDelegate = (x) => { AppendMsg(this.msgHouse, user, msg); }; if (!this.msgHouse.IsDisposed) { this.msgHouse.Invoke(actionDelegate, msg); } } else { AppendMsg(this.msgHouse, user, msg); } }
/// <summary> /// 消息格式 /// </summary> /// <returns></returns> private void AppendMsg(RichTextBox richTextBox, User user, object msg_body) { if (!richTextBox.IsDisposed) { richTextBox.SelectionColor = user.titleColor; if (richTextBox.Text.Length != 0) { richTextBox.AppendText(" " + Environment.NewLine); } richTextBox.SelectionColor = user.titleColor; richTextBox.AppendText(string.Format("{0} {1}{2}", user.name, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), Environment.NewLine)); richTextBox.SelectionColor = user.msgColor; richTextBox.AppendText(string.Format("{0}{1}", msg_body.ToString(), Environment.NewLine)); } return; }
/// <summary> /// 发送消息给指定的用户 /// </summary> /// <param name="msg_type">消息类型</param> /// <param name="from_user">消息发送者</param> /// <param name="to_user">接收者</param> /// <param name="message">消息内容</param> /// <returns></returns> private bool SendToClient(CMD_TYPE msg_type, User from_user, User to_user, string message) { try { byte[] msg_bytes = Protocol.Protocol.MakeProtocolMsg(msg_type, from_user.MakeAuthCode(), to_user.MakeAuthCode(), message); to_user.bw.Write(msg_bytes); to_user.bw.Flush(); return true; } catch (ObjectDisposedException e) { return false; } catch (Exception e) { AddMsgHouseNewMsg(new User(), string.Format("服务端给{0}发送消息失败", to_user.name)); return false; } }
/// <summary> /// 发送给所有用户 /// </summary> /// <param name="from_user"></param> /// <param name="message"></param> private void SendToAllClient(CMD_TYPE cmd_type, string message, User from_user = null) { if (from_user == null) { from_user = new User(); } for (int i = 0; i < userList.Count; i++) { SendToClient(cmd_type, from_user, userList[i], message); } }
/// <summary> /// socket服务线程 /// </summary> private void SocketThread() { TcpClient newClient = null; while (true) { try { newClient = serverTCPListener.AcceptTcpClient(); newClient.SendTimeout = 500; } catch (Exception e) { break; } //每接收一个客户端连接,就创建一个对应的线程循环接收该客户端发来的信息; User user = new User(newClient); Thread threadReceive = new Thread(ReceiveDataThread); threadReceive.Start(user); userList.Add(user); AddClientListBoxItem(user); UpdateTotalNum(this.userList.Count); } }