private void StartListening() { while (true) { try { byte[] buffer = new byte[1024]; int bytesRec = socket.Receive(buffer); string data = Encoding.UTF8.GetString(buffer, 0, bytesRec); Message message = Message.Parse(data); Console.WriteLine(message.Data); switch (message.MessageType) { case Message.Type.USUAL_MESSAGE: server.SendAll(message); break; case Message.Type.PRIVATE_MESSAGE: server.SendPrivate(message); break; } } catch (Exception e) { break; } } }
private static string HandleMessage(string data) { Console.WriteLine("Received message"); var msg = new Message(); msg.Parse(data); Console.WriteLine("Parsed message : {0}", msg.MessageType()); Console.WriteLine("Message timestamp : {0}", msg.MessageDateTime()); Console.WriteLine("Message control id : {0}", msg.MessageControlId()); // ********************************************************************* // Here you could do something usefull with the received message ;-) // ********************************************************************* // todo // Create a response message // var response = new Message(); var msh = new Segment("MSH"); msh.Field(2, "^~\\&"); msh.Field(7, DateTime.Now.ToString("yyyyMMddhhmmsszzz")); msh.Field(9, "ACK"); msh.Field(10, Guid.NewGuid().ToString()); msh.Field(11, "P"); msh.Field(12, "2.5.1"); response.Add(msh); var msa = new Segment("MSA"); msa.Field(1, "AA"); msa.Field(2, msg.MessageControlId()); response.Add(msa); // Put response message into an MLLP frame ( <VT> data <FS><CR> ) // var frame = new StringBuilder(); frame.Append((char)0x0B); frame.Append(response.Serialize()); frame.Append((char)0x1C); frame.Append((char)0x0D); return(frame.ToString()); }
private static async Task HandleMessage(Message message, Stream stream) { // This is where the client can create requests about specifik data. switch (message.Action) { case Action.ChatMessage: var memberMessage = message.Parse <ChatMessage>(); await MessageOtherClientsAsync(Create(Action.ChatMessage, memberMessage), stream); break; case Action.PrivateChatMessage: var pm = message.Parse <PrivateMessage>(); // If this got a value it means that the user is trying to whisper another user. var maybeUser = Users .SingleOrDefault(user => user.Name.Equals(pm.Recipent)) // Could be null .ToMaybe() // Do the following functions as long as user is not null .Select(user => user.Client.GetStream()); // If this got a value it means that the user is trying to whisper himself. var maybeSelf = maybeUser .Where(rStream => rStream.Equals(stream)); await maybeSelf .SelectOrElse( //Client whispered himself rStream => SendPm(rStream, "You just whispered yourself", Server), () => maybeUser.SelectOrElse( // Client whispered a different client rStream => SendPm(rStream, pm.Message, pm.Recipent), // Client whispered a none existing client. () => SendPm(stream, "PM: There's no user with that name", Server) ) ); break; } }
public void Activate() { serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse(ipaddress), port); serverSocket.Bind(ipPoint); serverSocket.Listen(10); NotifyAll("Server is online.\n IP Adress: " + ipaddress + ". Port: " + port); running = true; try { while (true) { Socket clientSocket = serverSocket.Accept(); byte[] buffer = new byte[1024]; int bytesRec = clientSocket.Receive(buffer); string data = Encoding.UTF8.GetString(buffer, 0, bytesRec); Message message = Message.Parse(data); string user = message.From; Console.WriteLine(message.Data); ClientHandler client = new ClientHandler(this, clientSocket, user); Clients.Add(client); SendAll(new Message(user + " connected", "Server")); NotifyAll(user + " connected"); SendUserList(); } } catch { NotifyAll("Connection break"); } }