private void SocketClient_ReceiveData(object sender, SocketReceiveEventArgs e) { var request = Encoding.UTF8.GetString(e.Data, 0, e.Length); if (request.StartsWith("MESSAGE:", StringComparison.OrdinalIgnoreCase)) { var tokens = request.Split(':'); var name = tokens[1]; var message = tokens[2]; Receive?.Invoke( this, new ChatMessageEventArgs { Message = new ChatMessage { Name = name, Text = message } }); } }
private void SocketServer_ReceiveData(object sender, SocketReceiveEventArgs e) { var requestMessage = Encoding.UTF8.GetString(e.Data, 0, e.Length); Console.WriteLine($"Text: {requestMessage}"); if (requestMessage.StartsWith("LOGINNAME:", StringComparison.OrdinalIgnoreCase)) { var tokens = requestMessage.Split(':'); m_ClientNames.TryAdd(e.Client, tokens[1]); Console.WriteLine("...and the client name is: " + tokens[1]); } else if (requestMessage.StartsWith("BROADCAST:", StringComparison.OrdinalIgnoreCase)) { var tokens = requestMessage.Split(':'); var message = tokens[1]; var broadcastMessage = $"MESSAGE:{m_ClientNames[e.Client]}:{message}"; var buffer = Encoding.UTF8.GetBytes(broadcastMessage); m_SocketServer.BroadcastAsync(e.Client, buffer); } }