public void SendGlobalMessage_SuccessTest(string nickname, string message)
        {
            TcpClient tcpClient = new TcpClient();
            string    ip        = ServerChatServiceConstants.MANUAL_IP;

            tcpClient.Connect(ip, ServerChatServiceConstants.SERVER_PORT);

            var addUser = serverChatService.AddUser(tcpClient, nickname);

            var removeUser = serverChatService.SendMessage(nickname, message);

            Assert.True(removeUser.Success);
        }
Пример #2
0
 private void SendMessage()
 {
     _serverChat.SendMessage(YourMessage, Recipient);
     Messages.Add($"You: {YourMessage}");
     YourMessage = string.Empty;
     RaisePropertyChanged("YourMessage");
 }
Пример #3
0
        private void sendButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(messageTextbox.Text))
            {
                MessageBox.Show("Please enter a message first.");
                return;
            }

            var selectedClientRecipient = clientsListbox.SelectedItem as ConnectedClient;

            if (selectedClientRecipient == null)
            {
                MessageBox.Show("Please select a recipient first.");
                return;
            }

            _serverChannel.SendMessage(_userId, selectedClientRecipient.Id, messageTextbox.Text);
        }
        /// <summary>
        /// Accept the Client inserting him in the Server Hash Tables and starts
        /// to listening user messages.
        /// </summary>
        private void AcceptClient()
        {
            try
            {
                Reader = new System.IO.StreamReader(TcpClient.GetStream());
                Writer = new System.IO.StreamWriter(TcpClient.GetStream());

                // Read the user information
                UserInformation = Reader.ReadLine();

                // If the information exists
                if (UserInformation != "")
                {
                    // Check if the user already exists in the current connection
                    if (ServerChatService.Users.Contains(UserInformation) == true)
                    {
                        // 0: not connected
                        Writer.WriteLine("0|This is nickname already exists in the chat.");
                        Writer.Flush();
                        CloseConnection();
                        return;
                    }
                    else if (UserInformation == "Administrator")
                    {
                        // 0: not connected
                        Writer.WriteLine("0|Wow! You can't be the Administrator! hehe");
                        Writer.Flush();
                        CloseConnection();
                        return;
                    }
                    else
                    {
                        // 1: Connected successfully
                        Writer.WriteLine("1");
                        Writer.Flush();

                        // Add the user in the HashTable and starts the message listener


                        var user = _serverChatService.AddUser(TcpClient, UserInformation);

                        if (!user.Success)
                        {
                            throw new Exception("Error when trying to add the new user.");
                        }
                    }
                }
                else
                {
                    CloseConnection();
                    return;
                }

                try
                {
                    // Still waiting for new user messages
                    while ((Response = Reader.ReadLine()) != "")
                    {
                        // If invalid, remove the user
                        if (Response == null)
                        {
                            _serverChatService.RemoveUser(TcpClient);
                        }
                        else
                        {
                            // If OK, send the message to the Server treatment
                            var message = _serverChatService.SendMessage(UserInformation, Response);

                            if (!message.Success)
                            {
                                throw new Exception("Error when trying to send the message. ");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine("*** Exception Warning:");
                    Console.WriteLine(ex.Message);
                    Console.ResetColor();

                    // If any problem with the user, user is disconnected
                    _serverChatService.RemoveUser(TcpClient);
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine("*** Exception Warning:");
                Console.WriteLine(ex.Message);
                Console.ResetColor();
            }
        }