private void ExitChat() { Message message = new Message() { Content = $"*{userName} has left the chat*", SenderPort = localPort, Type = messageType.Leave }; byte[] data = ByteParser.ObjToByteArray(message); udpClient.Send(data, data.Length, host, remotePort); udpClient.DropMulticastGroup(groupAddress); online = false; udpClient.Close(); chatTextBlock.Inlines.Add($"\n({DateTime.Now.ToShortTimeString()}) *You has left the chat*"); loginButton.IsEnabled = true; logoutButton.IsEnabled = false; sendButton.IsEnabled = false; saveButton.IsEnabled = true; userNameTextBox.IsReadOnly = false; localPortTextBox.IsReadOnly = false; remotePortTextBox.IsReadOnly = false; hostAddressTextBox.IsReadOnly = false; }
private void sendButton_Click(object sender, EventArgs e) { if (messageTextBox.Text.Trim().Length == 0) { return; } try { Message message = new Message() { Content = $"{userName}: {messageTextBox.Text.Trim()}", SenderPort = localPort, Type = messageType.Message }; byte[] data = ByteParser.ObjToByteArray(message); udpClient.Send(data, data.Length, host, remotePort); messageTextBox.Clear(); scrollViewer.ScrollToEnd(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void loginButton_Click(object sender, EventArgs e) { if (userNameTextBox.Text.Trim().Length == 0) { userNameTextBox.Clear(); return; } userName = userNameTextBox.Text.Trim(); userNameTextBox.IsReadOnly = true; try { udpClient = new UdpClient(localPort); udpClient.JoinMulticastGroup(groupAddress, TTL); Task receiveTask = new Task(ReceiveMessages); receiveTask.Start(); Message message = new Message() { Content = $"*{userName} joined the chat*", SenderPort = localPort, Type = messageType.Join }; byte[] data = ByteParser.ObjToByteArray(message); udpClient.Send(data, data.Length, host, remotePort); localPortTextBox.IsReadOnly = true; remotePortTextBox.IsReadOnly = true; hostAddressTextBox.IsReadOnly = true; saveButton.IsEnabled = false; loginButton.IsEnabled = false; logoutButton.IsEnabled = true; sendButton.IsEnabled = true; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void ReceiveMessages() { online = true; try { while (online) { IPEndPoint remoteIp = null; byte[] data = udpClient.Receive(ref remoteIp); Message message = (Message)ByteParser.ByteArrayToObj(data); switch (message.Type) { case messageType.Confirm: Dispatcher.Invoke(new Action(delegate { chatTextBlock.Inlines.Add($"\n*message #{message.Id} delivered*"); })); break; case messageType.Message: Dispatcher.Invoke(new Action(delegate { if (message.SenderPort == localPort) { chatTextBlock.Inlines.Add($"\n[#{message.Id}]({DateTime.Now.ToShortTimeString()}) {message.Content}"); } else { chatTextBlock.Inlines.Add($"\n({DateTime.Now.ToShortTimeString()}) {message.Content}"); } })); Message responseMessage = new Message() { Type = messageType.Confirm, Id = message.Id, SenderPort = localPort }; byte[] newData = ByteParser.ObjToByteArray(responseMessage); udpClient.Send(newData, newData.Length, host, remotePort); break; case messageType.Join: Dispatcher.Invoke(new Action(delegate { if (chatTextBlock.Text.Length > 0) { chatTextBlock.Inlines.Add("\n"); } chatTextBlock.Inlines.Add($"({DateTime.Now.ToShortTimeString()}) {message.Content}"); })); break; case messageType.Leave: Dispatcher.Invoke(new Action(delegate { chatTextBlock.Inlines.Add($"\n({DateTime.Now.ToShortTimeString()}) {message.Content}"); })); break; case messageType.ServerShutDown: Dispatcher.Invoke(new Action(delegate { ExitChat(); MessageBox.Show("Server shut down"); chatTextBlock.Inlines.Clear(); })); break; } } } catch (ObjectDisposedException) { if (!online) { return; } throw; } catch (SocketException ex) when(ex.ErrorCode == 10004) { return; } catch (Exception ex) { MessageBox.Show(ex.Message); } }