Ejemplo n.º 1
0
        private void lbUsers_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ListBox boxItem = (ListBox)sender;

            foreach (var item in chatSessionsList) //
            {
                string nameInListBox = dataset[boxItem.SelectedIndex];
                if (item.message_to == nameInListBox)
                {
                    SystemSounds.Beep.Play();
                    //rtbNotificatons.SelectionBrush = System.Windows.Media.Color;
                    rtbNotificatons.AppendText("Chat Session with " + nameInListBox + "is already running " + Environment.NewLine);
                    return;
                }
            }

            //Pass name of the user to chat with
            ChatSessionWindow chatWindow = new ChatSessionWindow(dataset[boxItem.SelectedIndex], user, new Random().Next(1000, 100000));

            chatSessionsList.Add(chatWindow);
            chatWindow.sendAttributesToServer(user.MySocket);
            chatWindow.Show();
        }
Ejemplo n.º 2
0
        private void Worker_DoWork_ReceiverInput(object sender, DoWorkEventArgs e)
        {
            //to store names for data source input of ListBox
            BindingList <String> names = new BindingList <String>();

            byte[]         buffer       = new byte[255];
            UserConnection user         = (UserConnection)e.Argument;
            string         chunk        = String.Empty;
            string         dataReceived = String.Empty;

            while (true)
            {
                do
                {
                    if (user.MySocket.Connected)
                    {
                        int rec;
                        try
                        {
                            rec = user.MySocket.Receive(buffer);//, 0, buffer.Length, 0);
                        }
                        catch (SocketException)
                        {
                            return;
                        }
                        Array.Resize(ref buffer, rec); // Resize Array
                        chunk         = Encoding.Default.GetString(buffer);
                        dataReceived += chunk;
                    }
                } while (user.MySocket.Available > 0);

                if (dataReceived.StartsWith("notificationOfUsers"))
                {
                    this.Dispatcher.Invoke(new Action(() => {
                        names.Clear();
                    }));
                    //if it is a notification parse it into strings
                    foreach (String nameOfUser in dataReceived.Split(' '))
                    {
                        this.Dispatcher.Invoke(new Action(() =>
                        {
                            //do not add itself in list of notification
                            if (nameOfUser != user.Username && nameOfUser != "notificationOfUsers" && nameOfUser.Trim() != "")
                            {
                                if (!names.Contains(nameOfUser) && nameOfUser != user.Username)
                                {
                                    names.Add(nameOfUser);
                                    rtbNotificatons.AppendText(nameOfUser + "  Joinded Chat\n");
                                }
                            }
                        }));
                    }
                }
                else if (dataReceived.StartsWith("newSessionStarted"))
                {
                    string[] parsedMessage = dataReceived.Split(':');
                    //from(to) --
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        ChatSessionWindow newWindow = new ChatSessionWindow(parsedMessage[1], user, int.Parse(parsedMessage[3]));
                        chatSessionsList.Add(newWindow);
                        newWindow.Show();
                    }));
                }
                else if (dataReceived.Trim().StartsWith("Logout"))
                {
                    string[] parsedMessage = dataReceived.Split(':');
                    dataset.Remove(parsedMessage[1]);

                    // remove all the elements containg the name of loggedOutUser and notify
                    foreach (var item in chatSessionsList)
                    {
                        //matches logged Out User to any of session's target name
                        if (parsedMessage[1] == item.message_to)
                        {
                            chatSessionsList.Remove(item);
                            this.Dispatcher.Invoke(() =>
                            {
                                item.Close();
                            });
                            break;
                        }
                    }

                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        lbUsers.GetBindingExpression(ListBox.ItemsSourceProperty).UpdateTarget();
                        rtbNotificatons.AppendText(parsedMessage[1] + " Logged Out" + "\n");
                    }));
                    //TODO:Close all the windows containg the same username also
                }
                //There is a message to send
                else
                {
                    string[] parsedMessage = dataReceived.Split(':');
                    foreach (var chatUser in chatSessionsList)
                    {
                        if (chatUser.uniqueIdentifier == int.Parse(parsedMessage[0]))
                        {
                            this.Dispatcher.Invoke(new Action(() =>
                            {
                                chatUser.receivedMessage(parsedMessage[2] + ":" + parsedMessage[3] + "\n");
                            }));
                        }
                    }
                }//#if-else-if end

                this.Dispatcher.Invoke(new Action(() =>
                {
                    dataset = names;
                }));

                chunk        = String.Empty;
                dataReceived = String.Empty;

                (sender as BackgroundWorker).ReportProgress(1);     //report Progress changed
                try
                {
                    if (!(user.MySocket.Available > 0))
                    {
                        //if no data is available to read
                        Thread.Sleep(2000);
                    }
                }
                catch (ObjectDisposedException)
                {
                    return;
                }
                //  }//#end-lock

                //If There is a message send to the CLient
                //ChatSessionWindow.SendMessage("Data");
            }//#end-while
        }