private void btDisconnect_Click(object sender, EventArgs e) { client?.SendMessage(new LANMessage(MessageType.ClientExit, client.ClientId, tbName.Text)); client?.Disconnect(); client = null; btDisconnect.Enabled = false; btSendMessage.Enabled = false; foreach (DialogInfo dialog in communityData.Dialogs.Values) { dialog.IsActive = false; } UpdateParticipants(); btConnect.Enabled = true; cbIsConnected.Checked = false; tbName.ReadOnly = false; tbPassword.ReadOnly = false; }
public void Test_SendMessage_Received() { var mockConnection = Substitute.For <IMessageSender>(); var client = new ChatClient.Client(mockConnection); var message = "Hello World"; client.SendMessage(message); mockConnection.Received().SendMessage(message); }
private void btSend_Click(object sender, EventArgs e) { client.SendMessage(client.Login, tbMessage.Text); }
static void Main(string[] args) { failedToConnect :; Console.Write("Enter the IP address to connect to:\n> "); var ip = Console.ReadLine(); invalidPort :; Console.Write("Enter the port to connect to:\n> "); var portString = Console.ReadLine(); Console.WriteLine(); if (int.TryParse(portString, out var port)) { try { client = new Client(ip, port); if (client.FailedConnect) { Console.WriteLine("Failed to connect!"); goto failedToConnect; } client.MessageReceived += Client_MessageReceived; Console.WriteLine("Client connected."); while (!client.IsDisposed) { Console.Write("> "); var input = Console.ReadLine(); if (client.IsDisposed) { Console.WriteLine("The server closed. This client has disposed. Press any key to close..."); Console.ReadLine(); } else { if (input.ToLower().StartsWith("send ")) { var toSend = input.Substring(5, input.Length - 5); Console.WriteLine("Sending message: \n " + toSend + "\n"); client.SendMessage(toSend); Console.WriteLine("Sent message."); } else if (input.ToLower() == "stop") { Console.WriteLine("Disconnecting..."); client.Dispose(false); Console.WriteLine("Disconnected. Press any key to continue."); Console.ReadLine(); } } Console.WriteLine(); } } catch (Exception e) { Console.WriteLine("Error: " + e.Message); Console.ReadLine(); } } else { Console.WriteLine("Invalid Port. "); goto invalidPort; } }
public FormForMessages() { this.Size = new Size(670, 510); listOfMessages.Bounds = new Rectangle(new Point(40, 40), new Size(400, 370)); listOfMessages.View = View.Details; listOfMessages.LabelEdit = true; listOfMessages.AllowColumnReorder = true; listOfMessages.FullRowSelect = true; listOfMessages.GridLines = true; listOfMessages.Sorting = SortOrder.Ascending; listOfMessages.Columns.Add("Participant", -2, HorizontalAlignment.Left); listOfMessages.Columns.Add("Message", -2, HorizontalAlignment.Left); Controls.Add(listOfMessages); listOfParticipants.Bounds = new Rectangle(new Point(470, 40), new Size(150, 370)); listOfParticipants.View = View.Details; listOfParticipants.LabelEdit = true; listOfParticipants.AllowColumnReorder = true; listOfParticipants.FullRowSelect = true; listOfParticipants.GridLines = true; listOfParticipants.Sorting = SortOrder.Ascending; listOfParticipants.Columns.Add("Participants", -2, HorizontalAlignment.Left); Controls.Add(listOfParticipants); var enterMeassageBox = new TextBox(); enterMeassageBox.Location = new Point(40, listOfMessages.Bottom); enterMeassageBox.Size = new Size(400, 100); Controls.Add(enterMeassageBox); var sendButton = new Button { Location = new Point(290, enterMeassageBox.Bottom), Size = new Size(150, 30), Text = "Send." }; Controls.Add(sendButton); sendButton.Click += (sender, args) => { Client.SendMessage(enterMeassageBox.Text); }; var disconnectButton = new Button { Location = new Point(40, enterMeassageBox.Bottom), Size = new Size(150, 30), Text = "Disconnect." }; Controls.Add(disconnectButton); disconnectButton.Click += (sender, args) => { Client.SendMessage("~Disconnect"); }; Client.MessageReceived += AddToWindow; Client.ParticipantConnected += AddToParticipantList; Client.ParticipantDisconnected += DeleteFromParticipantList; }