Ejemplo n.º 1
0
 public void Print(Message message)
 {
     this.Invoke(() =>
     {
         txtReceive.Text += "\n" + message.FromUser.Name + " says: " + message.MessageText;
     });
 }
        public void Print(Message message)
        {
            bool found = false;

            this.Invoke(() =>
            {
                Console.WriteLine("Message: " + message.MessageText);
                switch (message.MessageType)
                {
                case Message.Type.OneToMany:
                    txtReceive.AppendText("\r\n" + message.FromUser.Name + " says: " + message.MessageText);
                    txtReceive.ScrollToCaret();
                    break;

                case Message.Type.OneToOne:
                    foreach (var chat in privateChat)
                    {
                        if (!chat.Other.IpAddress.Equals(message.FromUser.IpAddress))
                        {
                            continue;
                        }
                        chat.txtReceive.AppendText("\r\n" + message.FromUser.Name + " says: " + message.MessageText);
                        chat.txtReceive.ScrollToCaret();
                        found = true;
                    }

                    if (!found)
                    {
                        foreach (var user in chats)
                        {
                            if (!user.IpAddress.Equals(message.FromUser.IpAddress))
                            {
                                continue;
                            }
                            found = true;
                        }
                        if (!found)
                        {
                            chats.Add(message.FromUser);
                        }
                        var chat = new PrivateChatUser(formLogin.Client, message.FromUser);
                        chat.txtReceive.AppendText("\r\n" + message.FromUser.Name + " says: " + message.MessageText);
                        chat.txtReceive.ScrollToCaret();
                        privateChat.Add(chat);
                    }

                    break;

                case Message.Type.PrivateChatConnect:
                    found = false;
                    foreach (var chat in privateChat)
                    {
                        if (!chat.Other.IpAddress.Equals(message.FromUser.IpAddress))
                        {
                            continue;
                        }
                        chat.Show();
                        found = true;
                    }

                    if (found)
                    {
                        break;
                    }
                    var chatNew = new PrivateChatUser(formLogin.Client, message.FromUser);
                    chatNew.Show();
                    privateChat.Add(chatNew);

                    break;

                case Message.Type.PrivateChatDisconnect:
                    found = false;
                    foreach (var chat in privateChat)
                    {
                        if (!chat.Other.IpAddress.Equals(message.FromUser.IpAddress))
                        {
                            continue;
                        }
                        chat.Hide();
                    }
                    break;
                }
            });
        }