// -------------------------------------------------- // Message Received // -------------------------------------------------- private static void MessageHandler_MessageReceived(object sender, MessageReceivedEventArgs e) { //The clients send a string as chat message so we just read it and store it string chatMessage = e.Message.ReadString(); //Display the chat message on the server Console.WriteLine($"{e.RemoteId}: {chatMessage}"); //Create an outgoing network message for the chat message NetOutMessage netMessage = new NetOutMessage(); netMessage.Write(chatMessage); netMessage.Finish(); //Now transmit the message to the other clients foreach (var item in server.Connections) { //We don't want to send the message back to the client that sent it if (item.Key != e.RemoteId) { //Send the message to a client item.Value?.Send(netMessage); } } }
static void Main(string[] args) { Console.WriteLine("Amion.Network SimpleChat Client\n"); Console.WriteLine("Type '!connect' to connect to the server"); Console.WriteLine("Type '!help' for all commands\n"); //Create the network client client = new NetClient(); //Subscribe to Connection Added/Removed event client.ConnectionAdded += Client_ConnectionAdded; client.ConnectionRemoved += Client_ConnectionRemoved; //Create a message handler for incoming network messages messageHandler = new NetMessageHandler(); //Subscribe to the MessageReceived event where we will handle all incoming network messages messageHandler.MessageReceived += MessageHandler_MessageReceived; //Start the message processor thread messageHandler.StartMessageProcessor(); //Begin Loop to keep the program running bool exit = false; do { string command = Console.ReadLine(); switch (command) { case "!connect": { Console.WriteLine("Connecting to the server"); //Connect to IP: 127.0.0.1:6695 client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6695)); break; } case "!disconnect": { Console.WriteLine("Disconnecting from the server"); client.Disconnect(); break; } case "!exit": { Console.WriteLine("Exiting the program"); exit = true; break; } case "!help": { Console.WriteLine("Type '!exit' to quit the program"); Console.WriteLine("Type '!disconnect' to disconnect from the server"); Console.WriteLine("Type '!connect' to connect to the server"); break; } default: { //Create an outgoing network message for the chat message NetOutMessage netMessage = new NetOutMessage(); netMessage.Write(command); netMessage.Finish(); //Send the message if possible client.Connection?.Send(netMessage); break; } } } while (!exit); }