Esempio n. 1
0
        //查看消息
        private void button_msg_Click(object sender, EventArgs e)
        {
            //int index = comboBox_unread.SelectedIndex;
            int           index        = listView_unread.SelectedItems[0].Index;
            unread_Object selected_obj = unread_list[index];
            List <Socket> chat_socket  = new List <Socket>();
            //chat_socket.Add(selected_obj.unread_socket);
            List <string> all_msg  = new List <string>();;               //消息类型
            int           con_num  = 0;
            int           chat_num = selected_obj.user_list.Count() - 1; //聊天人数
            //unread_list.RemoveAt(index);
            //selected_obj.flag = false;
            //comboBox_unread.Items.Remove(index);
            //unread_num--;
            int i = 0;
            int m = 0;

            while (i < unread_list.Count())
            {
                if (unread_list[i].user_list.SequenceEqual(selected_obj.user_list))//如果用户列表相同
                {
                    chat_socket.Add(unread_list[i].unread_socket);

                    con_num++;
                    foreach (string msg in unread_list[i].unread_msg)
                    {
                        string[] split_msg = msg.Split('.');
                        if (split_msg[0] == "info")
                        {
                            for (int k = 1; k < split_msg.Length - 1; k++)
                            {
                                if (split_msg[k] == user_name)
                                {
                                    string temp  = split_msg[k];
                                    string temp2 = split_msg[1];
                                    split_msg[k] = split_msg[2]; //自己
                                    split_msg[1] = temp;
                                    split_msg[2] = temp2;        //消息发送方
                                    //split_msg[i] = temp2;
                                }
                            }
                            //unread_msg[count] = string.Join(".", split_msg);
                            //count++;
                        }
                        string new_msg = string.Join(".", split_msg);
                        all_msg.Add(new_msg);
                        // lengths = unread_list.unread_socket.EndReceive(ar);
                    }
                    unread_list[i].flag = false;
                    if (unread_list[i].msg_count != 0)
                    {
                        listView_unread.Items.RemoveAt(m);
                    }
                    unread_list.RemoveAt(i);
                    unread_num--;
                }
                else
                {
                    i++;
                    m++;
                }
            }
            button_msglist.Text = "新消息";
            if (unread_num == 0)
            {
                button_msg.Enabled = false;
            }
            Thread thread_chat = new Thread(() => Application.Run(new Chat_Window(all_msg, chat_socket, chat_num, con_num)));

            thread_chat.SetApartmentState(System.Threading.ApartmentState.STA);//单线程监听控制
            thread_chat.Start();
        }
Esempio n. 2
0
 //处理未读消息
 public void add_unread_record(string recv_str, Socket socket)
 {
     if (button_msg.InvokeRequired || label2.InvokeRequired || label2.InvokeRequired ||
         label1.InvokeRequired || button_refuse.InvokeRequired || button_approve.InvokeRequired ||
         listView1.InvokeRequired)
     {
         add_Unread_msg d = new add_Unread_msg(add_unread_record);
         this.Invoke(d, new object[] { recv_str, socket });
     }
     else
     {
         if (recv_str.Split('.')[0] == "a")//好友申请
         {
             string friend_name = recv_str.Split('.')[1];
             //Socket add_friend_socket = P2P_Communication.Commun_Friend(friend_name, client_socket);
             label1.Show();
             label2.Show();
             label2.Text = friend_name + " 请求添加好友";
             button_approve.Show();
             button_refuse.Show();
             button_approve.Enabled = true;
             button_refuse.Enabled  = true;
         }
         else if (recv_str.Split('.')[0] == "aa")//好友申请
         {
             string friend_name = recv_str.Split('.')[1];
             friend_list = friend_list + "." + friend_name;
             string insert_text = "update friend_table set friend_list = '" + user_name + "." + friend_list + "' where username="******"update friend_table set friend_list = '" + friend_list + "' where username="******"online");
             listView1.Items.Add(new_item);
             MessageBox.Show(this, friend_name + " 通过了你的好友申请", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         else if (recv_str.Split('.')[0] == "ar")
         {
             string friend_name = recv_str.Split('.')[1];
             MessageBox.Show(this, friend_name + " 拒绝了你的好友申请", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         else
         {
             //收到一条新的未读消息
             unread_Object new_obj = new unread_Object();
             //string user = recv_str.Replace(user_name, "");
             string        user      = recv_str.Substring(5);
             string[]      user_list = user.Split('.');
             List <string> all_user  = user_list.ToList();
             if (recv_str.Split('.')[0] == "info")
             {
                 new_obj.msg_count++;
             }
             all_user.Sort();//排序
             new_obj.user_list = all_user;
             all_user.RemoveAt(all_user.Count() - 1);
             //new_obj.unread_msg.Add(recv_str);
             new_obj.unread_socket = socket;
             new_obj.main_Window   = this;
             //new_obj.socket_count++;
             unread_list.Add(new_obj);
             P2P_Communication.Unread_Chat_Receive(unread_list[unread_num], unread_num);
             //Chat_Receive(unread_list[unread_num], unread_num);
             unread_num++;
             //string show_mess = info[1] + " " + info[info.Length - 1];
             if (recv_str.Split('.')[0] == "info")
             {
                 string sender = recv_str.Split('.')[1];
                 listView_unread.Items.Add(sender);
                 button_msglist.Text = unread_num.ToString();
                 button_msg.Enabled  = true;
             }
         }
     }
     //listView1.Items.Add(new_item);
 }