Exemplo n.º 1
0
        /// <summary>
        ///  Hàm xử lý dữ liệu nhận từ server khi chat
        /// </summary>
        /// <param name="chatting_o"></param>
        private void Receive_Message(Object chatting_o)
        {
            ChattingFromFriendList chatting = (ChattingFromFriendList)chatting_o;

            String received = sr.ReadLine(); //Cho bên server gởi line để tiện xử lý, bằng không thử ReadToEnd()

            sr.DiscardBufferedData();

            if (received.StartsWith("ChattingWithFriendOK"))
            {
                Image FriendAvatar;
                if (listisGotInfoFriend.ContainsKey(chatting.FriendUsername))
                {
                    FriendAvatar = listImageFriend.Images[chatting.FriendUsername];
                }
                else
                {
                    FriendAvatar = Image.FromStream(TCP_Connection.GetStream());
                }
                chatting.update_Avatar(chatting.Lvi_selected.SubItems[1].Text, FriendAvatar);
            }
            while (TCP_Connection.Connected) // Thay bằng khi còn connect
            {
                received = sr.ReadToEnd();
                //sr.DiscardBufferedData();
                chatting.update_ListBox(chatting.FriendUsername, received);
            }
        }
Exemplo n.º 2
0
        }//End of handle_received_LoadAllContent method

        /// <summary>
        ///  Xử lý sự kiện nhấp đôi vào 1 mục trong list friend, bật ra popupchat
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lstViewFriends_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //Lấy Item đã được chọn, nhầm có thông tin cần thiết
            ListViewItem selected = lstViewFriends.FocusedItem;
            // Khởi tạo mới, gởi qua gồm MyAvatar, ItemSelected, Friend Avatar, My Username cho constructor
            ChattingFromFriendList chatting = new ChattingFromFriendList(this.myUsername, this.myAvatar, selected, DefaultAvatar);

            if (!listChatting.Exists((chatting_object) =>
            {
                if (chatting_object.FriendUsername == null || "".Equals(chatting_object.FriendUsername))
                {
                    return(false);
                }
                if (chatting_object.FriendUsername.Equals(chatting.FriendUsername))
                {
                    return(false);
                }
                return(true);
            })) // Xem coi người bạn này đã được chọn và còn chat hay không
            {
                String init = "ChattingWithFriend-Username=" + selected.SubItems[1].Text;
                sw.Write(init);
                sw.Flush();

                listChatting.Add(chatting); // Thêm chatting này vào Collections

                // Liên kết dữ liệu gởi
                chatting.send_Message = new ChattingFromFriendList.Send_Message_Delegate(this.Send_Message);

                //Bắt đầu luồng nhận
                Thread Thread_Receive = new Thread(new ParameterizedThreadStart(Receive_Message));
                Thread_Receive.Start(chatting);
            }
            // Cho bật Form chat ra
            chatting.Show();
        }
Exemplo n.º 3
0
        private void handle_ListenAllMessage(object message)
        {
            String content = (String)message;

            if (content.StartsWith("FriendWantToChat-"))
            {
                Image        avatar          = Image.FromStream(TCP_Connection.GetStream());
                String[]     information     = content.Split('-'); // Split “FriendWantToChat-Username=username,Name=name,Avatar=”
                String[]     sub_information = information[1].Replace("Username="******"").Replace("Name=", "").Replace("Avatar=", "").Split(',');
                ListViewItem lvi             = new ListViewItem(sub_information[1]);
                lvi.Name = "Name";
                lvi.SubItems.Add(sub_information[0]).Name = "Username";
                if (this.myStatus.Equals(1)) // Nếu trạng thái của bản thân là online thì bật popup
                {
                    ChattingFromFriendList chatting = new ChattingFromFriendList(this.myUsername, this.myAvatar, lvi, avatar);

                    /*
                     * if (!listChatting.Exists((chatting_object) =>
                     * {
                     * if (chatting_object.FriendUsername == null || "".Equals(chatting_object.FriendUsername)) return false;
                     *  if (chatting_object.FriendUsername.Equals(chatting.FriendUsername)) return false;
                     *  return true;
                     * })) // Xem coi người bạn này đã được chọn và còn chat hay không
                     * { */
                    // Thêm chatting này vào Collections
                    listChatting.Add(chatting);
                    // Liên kết dữ liệu gởi
                    chatting.send_Message = new ChattingFromFriendList.Send_Message_Delegate(this.Send_Message);
                    //Bắt đầu luồng nhận
                    Thread Thread_Receive = new Thread(new ParameterizedThreadStart(Receive_Message));
                    Thread_Receive.Start(chatting);
                    chatting.Show();
                    sw.Write("FriendWantToChatOK-Username="******"ChangedStatus-"))
            {
                String[] information     = content.Split('-'); //Split “ChangedStatus-Username=username,Status=status”
                String[] sub_information = information[1].Replace("Username="******"").Replace("Status=", "").Split(',');

                ListViewItem new_lvi = lstViewFriends.Items[sub_information[0]];
                lstViewFriends.Items.RemoveByKey(sub_information[0]);

                new_lvi.SubItems.RemoveByKey("Status");
                String status;
                Color  status_Color;
                if (sub_information[1].Equals("-1"))
                {
                    status       = "Offline";
                    status_Color = SystemColors.AppWorkspace;
                }
                else if (sub_information[1].Equals("0"))
                {
                    status       = "Busy";
                    status_Color = Color.Red;
                }
                else
                {
                    status       = "Online";
                    status_Color = Color.Green;
                }
                new_lvi.SubItems.Add(status, status_Color, SystemColors.Control, lstViewFriends.Font).Name = "Status";
                lstViewFriends.Items.Add(new_lvi);
                return;
            }
            if (content.StartsWith("ChangedAvatar-"))
            {
                String[] information = content.Replace("Username="******"").Split('-'); // Split “ChangedAvatar-Username=username”
                Image    new_Avatar  = Image.FromStream(TCP_Connection.GetStream());
                listChatting.Find((chatting) =>
                {
                    if (chatting.FriendUsername.Equals(information[1]))
                    {
                        return(true);
                    }
                    return(false);
                }).update_Avatar(information[1], new_Avatar); // Cho update trên ô chat của username này
                if (listisGotInfoFriend.ContainsKey(information[1]))
                {
                    // Cập nhật lại trong image list của friend
                    listImageFriend.Images.RemoveByKey(information[1]);
                    listImageFriend.Images.Add(information[1], new_Avatar);
                }
                ListViewItem lvi = lstViewFriends.FocusedItem;
                if (lvi != null && lvi.SubItems["Username"].Equals(information[1])) // Đồng thời nếu user vừa đổi ảnh này trùng với trạng thái của user đó đang Focus thì cập nhật avatar ngay
                {
                    Bitmap scaled = new Bitmap(new_Avatar, pcBoxFriendAvatar.Width, pcBoxFriendAvatar.Height);
                    pcBoxFriendAvatar.Image = scaled;
                }
            }
        }