Пример #1
0
        //private Thread t;
        public FormNewRoom()
        {
            InitializeComponent();

            this.client = FormMain.client;

            //t = new Thread(new ParameterizedThreadStart(receiveMessage));
            //t.IsBackground = true;
        }
Пример #2
0
        public FormChatRoom(string name)
        {
            InitializeComponent();

            client = FormMain.client;
            this.chatRoomName = name;
            this.Text = chatRoomName + " - Chat Room";

            this.FormClosing += FormChatRoom_FormClosing;

            this.richTextBoxChat.LinkClicked += richTextBoxChat_LinkClicked;

            this.textBoxMessage.AllowDrop = true;
        }
Пример #3
0
        public FormChatPrivate(string nameSecondClient)
        {
            InitializeComponent();

            colleagueName = nameSecondClient;

            this.Text = colleagueName + " - Private Chat";

            client = FormMain.client;

            this.FormClosing += FormChatPrivate_FormClosing;
            this.richTextBoxChat.LinkClicked += richTextBoxChat_LinkClicked;
            this.textBoxMessage.AllowDrop = true;
        }
Пример #4
0
        public void receiveMessageFromServer()
        {
            // Boucle de réception des messages du serveur
            byte[] buf = new byte[ChatLib.ChatLib.MESSAGE_MAX_SIZE];
            ChatLib.MessageChat msg = null;

            while (true)
            {
                if (!finish)
                {
                    try
                    {
                        int nbBytes = client.ClientSocket.Receive(buf);

                        if (nbBytes > 0)
                        {
                            if (client.Connected && client.ClientSocket.Connected)
                            {
                                System.Diagnostics.Debug.WriteLine("FormMain received");

                                try
                                {
                                    msg = ChatLib.ChatLib.createMessageFromString(ChatLib.ChatLib.GetString(buf, nbBytes));
                                    System.Diagnostics.Debug.WriteLine(msg.MessageType);

                                    switch (msg.MessageType)// En fonction du type de message recu
                                    {

                                        case ChatLib.MessageType.ChatRoomExit:// Le serveur nous confirme que nous quittons la Chat Room
                                            if (msg.ContentMessage == "true")
                                            {
                                                this.Invoke((MethodInvoker)delegate
                                                {
                                                    listFormChatRoom.Remove(listFormChatRoom.Find(x => x.ChatRoomName == msg.TargetName));
                                                });
                                            }

                                            break;

                                        case ChatLib.MessageType.PrivateChatExit: // Le serveur nous confirme que nous quittons la Chat Room privée
                                            if (msg.ContentMessage == "true")
                                            {
                                                this.Invoke((MethodInvoker)delegate
                                                {
                                                    listFormChatPrivate.Remove(listFormChatPrivate.Find(x => x.PrivateChatName == msg.TargetName));
                                                });
                                            }
                                            break;

                                        case ChatLib.MessageType.ChatRoomJoin: // Le serveur nous confirme que nous nous connectons à une Chat Room
                                            if (msg.ContentMessage == "true")
                                            {
                                                this.Invoke((MethodInvoker)delegate
                                                {
                                                    if (!listFormChatRoom.Exists(x => x.ChatRoomName == msg.TargetName))
                                                    {
                                                        listFormChatRoom.Add(new FormChatRoom(msg.TargetName));
                                                    }

                                                    listFormChatRoom.Find(x => x.ChatRoomName == msg.TargetName).Show();
                                                });
                                            }
                                            break;

                                        case ChatLib.MessageType.ChatRoomCreate: // Le serveur nous confirme que nous avons créé une Chat Room
                                            if (msg.ContentMessage == "true")
                                            {
                                                this.Invoke((MethodInvoker)delegate
                                                {
                                                    if(!this.listFormChatRoom.Exists(x => x.ChatRoomName == msg.TargetName))
                                                    {

                                                        this.formNewRoom_OnCreateNewChatRoom(msg.TargetName);
                                                    }
                                                });
                                            }

                                            break;

                                        case ChatLib.MessageType.ClientChatRoomMessage: // A la reception d'un message
                                            // Verifier parmis toutes les Chat Rooms ouvertes dans lesquels le client est connecté
                                            // Transférer le message à cette chat room
                                            if (listFormChatRoom.Exists(x => x.ChatRoomName == msg.TargetName))
                                            {
                                                this.Invoke((MethodInvoker)delegate
                                                {
                                                    FormChatRoom chat = listFormChatRoom.Find(x => x.ChatRoomName == msg.TargetName);
                                                    chat.OnReceivedMessage(msg);
                                                });
                                            }

                                            break;

                                        case ChatLib.MessageType.ClientChatRoomImage: // A la reception d'un message de type image

                                            if (listFormChatRoom.Exists(x => x.ChatRoomName == msg.TargetName))
                                            {
                                                this.Invoke((MethodInvoker)delegate
                                                {
                                                    FormChatRoom chat = listFormChatRoom.Find(x => x.ChatRoomName == msg.TargetName);
                                                    chat.OnReceivedMessage(msg);
                                                });
                                            }

                                            break;

                                        case ChatLib.MessageType.UpdateConnectedUsersInChatRoom: // Le serveur nous informe que la liste des utilisateurs connectés à une Chat Room précise a été mise à jour

                                            if (listFormChatRoom.Exists(x => x.ChatRoomName == msg.TargetName))
                                            {
                                                this.Invoke((MethodInvoker)delegate
                                                {
                                                    FormChatRoom chat = listFormChatRoom.Find(x => x.ChatRoomName == msg.TargetName);
                                                    chat.UpdateConnectedUsersInChatRoom(msg);
                                                });
                                            }

                                            break;
                                        case ChatLib.MessageType.UpdateChatRoomList: // Le serveur nous indique que la liste des Chat Rooms a été mise à jour

                                            if (msg.ContentMessage != "false")
                                            {
                                                this.Invoke((MethodInvoker)delegate
                                                {
                                                    this.treeViewChatRooms.Nodes[0].Nodes.Clear(); // On vide la liste des Chat Rooms
                                                });

                                                foreach (string chatRoom in msg.ContentMessage.Split('\\'))
                                                {
                                                    // On réaffiche la liste des Chat Rooms
                                                    if (chatRoom.Length > 0)
                                                    {
                                                        this.Invoke((MethodInvoker)delegate
                                                        {
                                                            this.formNewRoom_OnCreateNewChatRoom(chatRoom);
                                                        });
                                                    }

                                                }

                                            }
                                            if (msg.ContentMessage == "false")
                                            {
                                                MessageBox.Show("You can't delete this Chat Room while users are still in it.");
                                            }

                                            break;

                                        case ChatLib.MessageType.PrivateChatCreate: // Le serveur nous confirme la création d'une Chat Room privée

                                            FormChatPrivate privchat = new FormChatPrivate(msg.SenderName);
                                            if (!this.listFormChatPrivate.Exists(x => x.PrivateChatName == msg.SenderName))
                                            {
                                                this.Invoke((MethodInvoker)delegate
                                                {
                                                    this.listFormChatPrivate.Add(privchat);
                                                    this.listFormChatPrivate.Find(x => x.PrivateChatName == privchat.PrivateChatName).Show();
                                                });
                                            }
                                            break;

                                        case ChatLib.MessageType.ClientPrivateMessage: // A la réception d'un message privé

                                            if (listFormChatPrivate.Exists(x => x.PrivateChatName == msg.SenderName))
                                            {
                                                this.Invoke((MethodInvoker)delegate
                                                {
                                                    FormChatPrivate chat = listFormChatPrivate.Find(x => x.PrivateChatName == msg.SenderName);
                                                    chat.OnReceivedMessage(msg);
                                                });
                                            }
                                            break;
                                        case ChatLib.MessageType.ClientPrivateImage: // A la réception d'un message privé de type image

                                            if (listFormChatPrivate.Exists(x => x.PrivateChatName == msg.SenderName))
                                            {
                                                this.Invoke((MethodInvoker)delegate
                                                {
                                                    FormChatPrivate chat = listFormChatPrivate.Find(x => x.PrivateChatName == msg.SenderName);
                                                    chat.OnReceivedMessage(msg);
                                                });
                                            }
                                            break;
                                        case ChatLib.MessageType.UpdateConnectedUsers: // Le serveur nous indique que la liste des utlisateurs connectés a été mise à jour
                                            string[] users = msg.ContentMessage.Split('\\');

                                            this.Invoke((MethodInvoker)delegate
                                               {
                                                   this.treeViewClients.Nodes[0].Nodes.Clear(); // On vide la liste des utilisateurs connectés

                                                   foreach (string s in users)
                                                   {
                                                       System.Diagnostics.Debug.WriteLine(s);

                                                       if (s.Length > 0 && s != client.UserName)
                                                       {
                                                           this.treeViewClients.Nodes[0].Nodes.Add(new TreeNode(s));
                                                       }
                                                   }

                                                   this.treeViewClients.ExpandAll();
                                               });
                                            break;

                                        case ChatLib.MessageType.DisconnectFromServer: // Le serveur nous confirme notre déconnexion
                                            if (msg.ContentMessage == "true")
                                            {
                                                FormMain.client = null;

                                                finish = true;
                                                if (!this.IsDisposed)
                                                {
                                                    this.Invoke((MethodInvoker)delegate
                                                  {
                                                      this.Text = "Chat Application";
                                                      this.menuStrip_Update();
                                                  });
                                                }

                                                return;
                                            }
                                            break;

                                    }
                                }
                                catch (JsonReaderException)
                                {
                                }
                            }
                            else return;
                        }
                    }
                    catch (SocketException)
                    {
                    }
                }
                else return;
            }
        }
Пример #5
0
        /*********************/
        /***** DELEGATES *****/
        private void formConnection_OnCreateConnectingClient(Client client)
        {
            if (client != null)
            {
                FormMain.client = client;
                this.Text = FormMain.client.UserName + " - Chat Application";

                t = new Thread(new ThreadStart(receiveMessageFromServer));
                t.IsBackground = false;
                t.Start();
            }
            else
            {
                MessageBox.Show("Error while trying to connect");
            }
        }