示例#1
0
        private void sendButton_Click(object sender, RoutedEventArgs e)
        {
            Message messageToSend  = new Message(Message.Header.POST);
            string  encryptMessage = DESAlg.Encrypt(messageTextBox.Text);

            messageToSend.addData(encryptMessage);
            client.sendMessage(messageToSend);
            messageTextBox.Clear();
        }
示例#2
0
        /// <summary>
        /// Deal with the message received
        /// </summary>
        /// <param name="message"></param>
        private void processData(Message message)
        {
            switch (message.Head)
            {
            case Message.Header.REGISTER:
                if (message.MessageList[0] == "success")
                {
                    Console.WriteLine("- Registration success: " + User.Login);
                }
                else
                {
                    Console.WriteLine("- Registration failed: " + User.Login);
                }
                break;

            case Message.Header.JOIN:
                if (message.MessageList[0] == "success")
                {
                    Logged = true;
                    Console.WriteLine("- Connection success: " + User.Login);
                }
                else
                {
                    Logged = false;
                    Console.WriteLine("- Connection failed: " + User.Login);
                }
                break;

            case Message.Header.QUIT:
                Quit   = true;
                Logged = false;
                Console.WriteLine("Message.Header.QUIT : Server disconnected");
                break;

            case Message.Header.JOIN_CR:
                if (message.MessageList[0] == "success")
                {
                    // Clear current messages
                    messagesBindingList.Clear();

                    User.Chatroom = new Chatroom(message.MessageList[0]);
                    Console.WriteLine("- Joined chatroom: " + message.MessageList[1]);
                }
                else
                {
                    Console.WriteLine("- Could not join chatroom: " + message.MessageList[1]);
                }
                break;

            case Message.Header.QUIT_CR:
                if (message.MessageList[0] == "success")
                {
                    User.Chatroom = null;
                    Console.WriteLine("- Chatroom left: " + message.MessageList[1]);
                }
                else
                {
                    Console.WriteLine("- Could not leave chatroom : " + message.MessageList[1]);
                }
                break;

            case Message.Header.CREATE_CR:
                if (message.MessageList[0] == "success")
                {
                    sendMessage(new Message(Message.Header.LIST_CR));
                    Console.WriteLine("- Chatroom created: " + message.MessageList[1]);
                }
                else
                {
                    Console.WriteLine("- Could not create chatroom: " + message.MessageList[1]);
                }
                break;

            case Message.Header.LIST_CR:

                foreach (string chatroom in message.MessageList.ToList())
                {
                    if (chatroomsBindingList.ToList().Any(x => x.Name == chatroom) == false)
                    {
                        chatroomsBindingList.Add(new Chatroom(chatroom));
                    }
                }
                break;

            case Message.Header.LIST_USERS:
                usersBindingList.Clear();

                foreach (string user in message.MessageList)
                {
                    usersBindingList.Add(new User(user));
                }
                break;

            case Message.Header.POST:
                Console.WriteLine("- Message received: (" + message.MessageList[0] + ") " + message.MessageList[1]);

                if (message.MessageList[1].Contains("joined the chatroom") || message.MessageList[1].Contains("left the chatroom"))
                {
                    messagesBindingList.Add(message.MessageList[1]);
                }
                else
                {
                    RSACryptoServiceProvider RSAKey = new RSACryptoServiceProvider();
                    var    message1    = new Message(Message.Header.POST);
                    int    length      = message.MessageList[1].Length;
                    string splitString = message.MessageList[1].Substring(message.MessageList[1].IndexOf(":") + 1);
                    string decrypted   = DESAlg.Decrypt(splitString);

                    message.MessageList[1] = message.MessageList[0] + ": " + decrypted;
                    messagesBindingList.Add(message.MessageList[1]);
                }
                break;
            }
        }