Ejemplo n.º 1
0
        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();
        }
Ejemplo n.º 2
0
        private static void InitializeTcpServer(int portNumber)
        {
            var callback = new ChatCallback
            {
                OnClientConnected = endPoint =>
                {
                    Console.WriteLine("Nowy klient połączony: {0}:{1}", endPoint.Address, endPoint.Port);
                },
                OnMessageReceived = (tcpUser, message) =>
                {
                    var endPoint         = (IPEndPoint)tcpUser.Socket.RemoteEndPoint;
                    var formattedMessage = MessageExtractor.Extract(message);
                    var information      = InformationConverter.Deserialize(formattedMessage);
                    if (information?.Type == Message)
                    {
                        Console.WriteLine(
                            $"{information.Nick} ({endPoint.Address}:{endPoint.Port}) : {information.Message}");
                    }
                },
                OnClientDisconnected = endPoint =>
                {
                    Console.WriteLine($"({endPoint.Address}:{endPoint.Port}) has disconnected!");
                }
            };
            var server = new TCPServer(portNumber, callback);

            server.Initialize();
        }
Ejemplo n.º 3
0
 private static ChatCallback CreateChatCallback()
 {
     return(new ChatCallback
     {
         OnConnected = () =>
         {
             _isConnected = true;
             Console.WriteLine("Zostałeś połączony z serwerem. Możesz zacząć pisać!");
         },
         OnMessageReceived = message =>
         {
             var formattedMessage = MessageExtractor.Extract(message);
             var information = InformationConverter.Deserialize(formattedMessage);
             if (information.Type == Message)
             {
                 Console.WriteLine($"{information.Nick} : {information.Message}");
             }
         },
         OnCannotConnect = () =>
         {
             _isConnected = false;
             Console.WriteLine("Brak połączenia z serwerem. Sprawdź dane połączenia oraz spróbuj ponownie.");
         }
     });
 }
        public void ShouldThrowJsonReaderExceptionWhenJsonIsOnlyWhitespace()
        {
            var json = "      ";

            Action act = () => InformationConverter.Deserialize(json);

            act.Should().Throw <ArgumentException>();
        }
        public void ShouldThrowJsonReaderExceptionWhenJsonIsMalformed()
        {
            var json = "{hello:World}";

            Action act = () => InformationConverter.Deserialize(json);

            act.Should().Throw <JsonReaderException>();
        }
        public void ShouldThrowJsonReaderExceptionWhenJsonIsIncorrect()
        {
            var json = "<hello>world</hello>";

            Action act = () => InformationConverter.Deserialize(json);

            act.Should().Throw <JsonReaderException>();
        }
        public void ShouldCorrectlyDeserialize(string json)
        {
            var information = InformationConverter.Deserialize(json);

            information.Nick.Should().Be("nick");
            information.Message.Should().Be("msg");
            information.Type.Should().Be(InformationType.Message);
        }