private void InitializeTcpClient() { var callback = new ChatCallback { OnConnected = () => Dispatcher.Invoke(() => PrintMessage("Pomyślnie połączono z " + IpAddress + ":" + Port)), OnMessageReceived = message => Dispatcher.Invoke(() => { var format = MessageExtractor.Extract(message); if (format == string.Empty) { return; } var msg = InformationConverter.Deserialize(format); if (mutedUsers.Contains(msg.Nick)) { return; } switch (msg.Type) { case InformationType.Message: PrintUserLine(msg.Nick, msg.Message); break; case InformationType.Voice: { if (!UsersSpeakingTextBlock.Text.Contains(msg.Nick)) { UsersSpeakingTextBlock.Text += "\uD83D\uDD0A" + msg.Nick + Environment.NewLine; } var byteMessage = Convert.FromBase64String(msg.Message); _audioManager.Play(byteMessage); break; } default: throw new ArgumentOutOfRangeException(); } }), OnDisconnected = () => Dispatcher.Invoke(() => { _client.Disconnect(); Environment.Exit(0); }), OnCannotConnect = () => Dispatcher.Invoke(() => { var formattedMessage = new ColoredMessageBuilder() .With("Nie można połączyć się z serwerem. Sprawdź dane połączenia.") .Build(); ChatBlock.Document.Blocks.Add(formattedMessage); ShowConnectionDialog(); ConnectToServer(); }) }; _client = new TCPClient(callback); ConnectToServer(); }
public void PrintMessage(string message) { var formattedMessage = new ColoredMessageBuilder() .With(message) .Build(); ChatBlock.Document.Blocks.Add(formattedMessage); ChatBlock.ScrollToEnd(); }
private void PrintUserLine(string user, string message) { if (string.IsNullOrWhiteSpace(message)) { return; } var formattedMessage = new ColoredMessageBuilder() .With(user + ": ", Brushes.RoyalBlue) .With(message) .Build(); ChatBlock.Document.Blocks.Add(formattedMessage); ChatBlock.ScrollToEnd(); }
private void InitializeAudioManager() { _audioManager = new AudioManager(OnDataAvailable); if (AudioManager.CanRecord()) { return; } RecordingButton.IsEnabled = false; RecordingButton.ToolTip = "Couldn't find any recording device in your system."; var formattedMessage = new ColoredMessageBuilder() .With("Couldn't find any recording device in your system.") .Build(); ChatBlock.Document.Blocks.Add(formattedMessage); }