Example #1
0
        private void clientSocket_CommandReceived(object sender, ChatLibrary.Message commandMessage)
        {
            if (commandMessage.MessageCommand.Equals(ChatLibrary.Command.ClientList))
            {
                string[] strClientList = commandMessage.MessageDetail.Split(new char[] { '@' });
                for (int i = 0; i < strClientList.Length; i++)
                {
                    if (!strClientList[i].Equals(clientSocket.Name))
                    {
                        chats.Add(new ChatModel {
                            Username = strClientList[i]
                        });
                    }
                }

                for (int i = 0; i < chats.Count; i++)
                {
                    if ((!strClientList.Contains(chats[i].Username)) &&
                        chats[i].Username != "Server" &&
                        chats[i].Username != Common.Conference)
                    {
                        chats.RemoveAt(i);
                        UpdateListBox();
                        i--;
                    }
                }
            }

            if (commandMessage.MessageCommand.Equals(ChatLibrary.Command.Logout))
            {
                RequestDisconnect();
            }
            UpdateListBox();
        }
Example #2
0
        private void clientSocket_MessageReceived(object sender, ChatLibrary.Message receivedMessage)
        {
            if (receivedMessage.MessageCommand.Equals(Command.PersonalMessage) ||
                receivedMessage.MessageCommand.Equals(Command.Conference)
                )
            {
                if (receivedMessage.Receiver.Equals(clientSocket.Name) || receivedMessage.Receiver.Equals(Common.All))
                {
                    bool bWindowFound = false;

                    foreach (var chat in chats)
                    {
                        if (chat.Username.Equals(receivedMessage.Sender))
                        {
                            ChoosenUser = receivedMessage.Sender;
                            OpenChat();

                            chat.Messages += WriteMessage(receivedMessage);
                            bWindowFound   = true;
                            break;
                        }
                    }
                    if (!bWindowFound)
                    {
                        CreateNewClientDelegate createNewClientDelegate = new CreateNewClientDelegate(CreateNewClient);
                        Dispatcher.Invoke(createNewClientDelegate, new object[] { receivedMessage });
                    }
                }
            }
            UpdateListBox();
        }
Example #3
0
 private void Announce(ChatLibrary.Message messageToAnnounce)
 {
     for (int i = 0; i < clientCollection.Count; i++)
     {
         if (!clientCollection[i].SendMessage(messageToAnnounce))
         {
             socketServer_ClientDisconnecting(this, clientCollection[i].Name);
         }
     }
 }
Example #4
0
        private void AddClientToList(Client client)
        {
            string strWriteMessage = WriteMessage("{0} Подключен", client.Name);

            ChatLibrary.Message connectionMessage = new ChatLibrary.Message();
            connectionMessage.Sender         = ChatLibrary.Common.ServerName;
            connectionMessage.Receiver       = ChatLibrary.Common.All;
            connectionMessage.MessageCommand = ChatLibrary.Command.PersonalMessage;
            connectionMessage.MessageDetail  = strWriteMessage;

            Announce(connectionMessage);
        }
Example #5
0
        private void GetMessage(ChatLibrary.Message message)
        {
            string time = message.SendTime.ToLongTimeString();
            string user = message.Nick;
            string text = message.Text;

            ListViewItem item = new ListViewItem(new[] { time, user, text });

            outputListView.Invoke(new Action(() =>
                                             outputListView.Items.Add(item)));

            outputListView.Items[outputListView.Items.Count - 1].EnsureVisible();
        }
Example #6
0
        private void CreateNewClient(ChatLibrary.Message clientMessage)
        {
            ChatModel chat = new ChatModel();

            chat.Username = clientMessage.MessageCommand.Equals(ChatLibrary.Command.Conference) ? ChatLibrary.Common.Conference : clientMessage.Sender;

            if (!clientMessage.MessageDetail.Equals(string.Empty))
            {
                chat.Messages += WriteMessage(clientMessage);
            }
            chats.Add(chat);

            UpdateListBox();
        }
Example #7
0
        private void SendClientList()
        {
            string strClientList = string.Empty;

            for (int i = 0; i < clientCollection.Count; i++)
            {
                strClientList += clientCollection[i].Name + "@";
            }
            strClientList = strClientList.TrimEnd(new char[] { '@' });

            ChatLibrary.Message clientListMessage = new ChatLibrary.Message();
            clientListMessage.Sender         = ChatLibrary.Common.ServerName;
            clientListMessage.Receiver       = ChatLibrary.Common.All;
            clientListMessage.MessageCommand = ChatLibrary.Command.ClientList;
            clientListMessage.MessageDetail  = strClientList;

            Announce(clientListMessage);
            PopulateClientList(strClientList);
        }
Example #8
0
        private void btnSendMessage_Click(object sender, RoutedEventArgs e)
        {
            if (lstClients.Items.Count > 0)
            {
                if (lstClients.SelectedIndex.Equals(-1))
                {
                    lstClients.SelectedIndex = 0;
                }

                ChatLibrary.Message messageToAnnounce = new ChatLibrary.Message();
                messageToAnnounce.Sender         = ChatLibrary.Common.ServerName;
                messageToAnnounce.Receiver       = lstClients.SelectedItem.ToString();
                messageToAnnounce.MessageCommand = ChatLibrary.Command.PersonalMessage;
                messageToAnnounce.MessageDetail  = WriteMessage(Message.Text);

                Announce(messageToAnnounce);
                Message.Clear();
            }
        }
Example #9
0
        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < clientCollection.Count; i++)
            {
                ChatLibrary.Message logoutMessage = new ChatLibrary.Message();
                logoutMessage.Sender         = ChatLibrary.Common.ServerName;
                logoutMessage.Receiver       = clientCollection[i].Name;
                logoutMessage.MessageCommand = ChatLibrary.Command.Logout;

                clientCollection[i].SendMessage(logoutMessage);
                clientCollection[i].Socket.Close();
                clientCollection.Remove(clientCollection[i].Name);
            }

            socketServer.Stop();

            lstClients.Items.Clear();
            WriteMessage("Сервер выключен!");
            SwitchItems();
        }
Example #10
0
        private string WriteMessage(ChatLibrary.Message clientMessage)
        {
            string strWriteText
                = string.Format("\r\n{0} {1}: {2}", GetTime(), clientMessage.Sender, clientMessage.MessageDetail);

            string strDisplayMessageType = string.Empty;

            if (clientMessage.MessageCommand.Equals(ChatLibrary.Command.PersonalMessage))
            {
                strDisplayMessageType = "Personal ";
            }
            else if (clientMessage.MessageCommand.Equals(ChatLibrary.Command.Conference))
            {
                strDisplayMessageType = "Conference ";
            }


            MessageToForm("\r\n" + strDisplayMessageType + strWriteText.TrimStart(new char[] { '\r', '\n' }));

            return(strWriteText);
        }
Example #11
0
        private void ListenForMessages(Client client)
        {
            while (true)
            {
                byte[] bytAcceptMessage = new byte[1024];
                try
                {
                    NetworkStream stream = client.Socket.GetStream();
                    stream.Read(bytAcceptMessage, 0, bytAcceptMessage.Length);
                }
                catch { break; }


                Message message = new ChatLibrary.Message(bytAcceptMessage);
                if (message.MessageCommand.Equals(Command.Login))
                {
                    if (ClientConnected != null)
                    {
                        client.Name = message.Sender;
                        ClientConnected(this, client);
                    }
                }
                if (message.MessageCommand.Equals(Command.PersonalMessage) ||
                    message.MessageCommand.Equals(Command.Conference)
                    )
                {
                    if (MessageReceived != null)
                    {
                        MessageReceived(this, message);
                    }
                }
                if (message.MessageCommand.Equals(Command.Logout))
                {
                    if (ClientDisconnecting != null)
                    {
                        ClientDisconnecting(this, message.Sender);
                    }
                }
            }
        }
Example #12
0
        private void buttonSend_Click(object sender, EventArgs e)
        {
            ChatLibrary.Message msg = new ChatLibrary.Message(me, textMessage.Text);

            if (_nameUserPrivateDialog != string.Empty)
            {
                string textWithoutDestination = textMessage.Text.Substring(_nameUserPrivateDialog.Length + 1);
                msg = new ChatLibrary.Message(me, textWithoutDestination, new User(_nameUserPrivateDialog, null));
                client.GetMessage(msg);
                //chatHistory.Add((ChatLibrary.Message) msg);  // Ликвидация задержки для собственного сообщения
                //messageHistory.Items.Add(msg);
                UpdateChat();
                textMessage.Text           = string.Format("/{0} ", _nameUserPrivateDialog);
                textMessage.SelectionStart = _nameUserPrivateDialog.Length + 2;
            }
            else
            {
                client.GetMessage(msg);
                //chatHistory.Add((ChatLibrary.Message) msg);  // Ликвидация задержки для собственного сообщения
                //messageHistory.Items.Add(msg);
                UpdateChat();
                textMessage.Clear();
            }
        }