// Start receiving messages from the server. // Handles client disconnection from the server. private void beginReceiving(TcpClientSocket client) { client.DataReceived += (ds, de) => { string receivedMessage = Encoding.ASCII.GetString(de.Payload); if (!RunCommand(receivedMessage, true)) { if (client.GetIpEndPointString().Contains("25565")) { RichTextBoxExtensions.AppendText(rtbClientMessages, $"\nServer: {Encoding.ASCII.GetString(de.Payload)}", Color.Blue); } else { AppendText($"{client.GetIpEndPointString()}: {Encoding.ASCII.GetString(de.Payload)}"); } } }; client.Disconnected += (ds, de) => { DisconnectFromServer(); }; }
private void beginReceiving(TcpClientSocket client) { client.DataReceived += (ds, de) => { string payloadString = Encoding.ASCII.GetString(de.Payload); // If the message starts with a special string, update the username if (payloadString.StartsWith("<c>username")) { client.Username = payloadString.Remove(0, 11); this.Invoke((MethodInvoker)(() => clbConnectedClients.Items.Add($"{client.Username}"))); AppendText($"{client.Username} has connected. ({client.GetIpEndPointString()})"); } else { TotalMessagesRecieved++; AppendText($"{client.Username}: {Encoding.ASCII.GetString(de.Payload)}"); TsslReceivedMessages.Text = $"Received Messages: {TotalMessagesRecieved}"; } }; client.Disconnected += (ds, de) => { try { // Output to the server console AppendText($"{client.Username} has disconnected!"); // Remove the socket from the checked list box clbConnectedClients.InvokeIfRequired(s => { s.Items.RemoveAt(this.serverSocket.GetClientListIndex(client)); }); // Remove the socket from the list of connected clients. this.serverSocket.ConnectedClients.Remove(client); // Displays how many clients are connected TsslConnectedClients.Text = $"Connected Clients: {this.serverSocket.ConnectedClients.Count}"; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }; }