public ChatForm(UdpUtils.Message otherInfo, UdpUtils.Message personalInfo) { InitializeComponent(); // 注册收到文件提示事件 UdpUtils.Client.ReceiveFileProgressNotify += ReceiveFileProgress; UdpUtils.Client.SendFileProgressNotify += SendFileProgress; OtherInfo = otherInfo; PersonalInfo = personalInfo; ShowUserInfo(); }
/// <summary> /// Send message to others /// </summary> /// <param name="msg"></param> public async void sendMessage(string msg) { string sendMsg = string.Format("{0}:{1}\r\n", PersonalInfo.FromUserName, msg); richTextBoxChat.AppendText(sendMsg); richTextBoxChat.ScrollToCaret(); chatContent.Append(sendMsg); UdpUtils.Message message = new UdpUtils.Message { FromUserName = PersonalInfo.FromUserName, ToUserName = lblPeerName.Text, Type = UdpUtils.MessageEnum.CHAT, Content = msg, SendTime = DateTime.Now }; await UdpUtils.Client.SendToClientAsync(lblPeerIP.Text, Convert.ToInt32(lblPeerPort.Text), message). ConfigureAwait(false); }
/// <summary> /// 完成拖放文件时触发此事件 /// </summary> private async void txtMessage_DragDrop(object sender, DragEventArgs e) { // 获取文件的路径 string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, false); StringBuilder sb = new StringBuilder(); foreach (string filePath in files) { sb.Append(filePath); } UdpUtils.Message message = new UdpUtils.Message { FromUserName = PersonalInfo.FromUserName, ToUserName = lblPeerName.Text, Type = UdpUtils.MessageEnum.FILE, SendTime = DateTime.Now, }; await UdpUtils.Client.SendFileToClient(lblPeerIP.Text, Convert.ToInt32(lblPeerPort.Text), sb.ToString(), message). ConfigureAwait(false); }
/// <summary> /// Connect to the server /// </summary> private async void btnConnection_Click(object sender, EventArgs e) { btnConnection.Enabled = txtIPAddress.Enabled = txtPort.Enabled = false; UdpUtils.Message message = new UdpUtils.Message() { FromUserName = this.Text, Type = UdpUtils.MessageEnum.SIGN_IN }; await UdpUtils.Client.SendToServerAsync(txtIPAddress.Text, Convert.ToInt32(txtPort.Text), message); this.Invoke(new MethodInvoker(() => { listViewOnlineUsers.Items.Add(note + "\r\n"); })); }
/// <summary> /// double-click the user info to chat /// </summary> private void listViewOnlineUsers_MouseDoubleClick(object sender, MouseEventArgs e) { string otherInfo = listViewOnlineUsers.SelectedItems[0].Text.Trim(); if (otherInfo == note) return; Match match = Regex.Match(otherInfo, @"^(\w+)-([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}):([0-9]+)$"); UdpUtils.Message otherProfile = new UdpUtils.Message() { FromUserName = match.Groups[1].Value, IpAddress = match.Groups[2].Value, Port = Convert.ToInt32(match.Groups[3].Value) }; if (otherInfo != note) { UdpUtils.Message myProfile = new UdpUtils.Message() { FromUserName = this.Text, IpAddress = "", Port = 0 }; ChatForm form = chatFormList.Find(f => f.Text == string.Format("{0}:{1}", this.Text, otherProfile.FromUserName)); if (form != null) form.Activate(); else { ChatForm chat = new ChatForm(otherProfile, myProfile); chatFormList.Add(chat); chat.Show(); } } }
/// <summary> /// close form /// </summary> private async void Main_FormClosing(object sender, FormClosingEventArgs e) { UdpUtils.Message message = new UdpUtils.Message() { FromUserName = this.Text, Type = UdpUtils.MessageEnum.SIGN_OUT }; await UdpUtils.Client.SendToServerAsync(txtIPAddress.Text, Convert.ToInt32(txtPort.Text), message); }