//处理未读消息 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); }
//发起聊天 private void button_chat_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count == 0 && listView_group.SelectedItems.Count == 0) { MessageBox.Show(this, " 请选中好友进行聊天", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (listView_group.SelectedItems.Count == 0 && listView1.SelectedItems[0].SubItems[1].Text == "offline") { MessageBox.Show(this, " 好友不在线,无法聊天", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { List <string> all_user = new List <string>(); List <Socket> chat_socket = new List <Socket>(); int chat_num = 0; //聊天人数 if (chat_select == 0) { string friend_name = user_name; foreach (ListViewItem item in listView1.SelectedItems) { friend_name = friend_name + "." + item.Text; chat_num++; } //开始发起聊天请求 string send_str = "chat." + friend_name; all_user.Add(send_str); if (chat_num > 1) { string select_text = "Select count(*) from group_table"; string group_name = "'chatgroup" + My_Database.SQLite_Select(select_text, connection) + "'"; //string group_name = "ccc"; string insert_text = "insert into group_table(groupname,username) values(" + group_name + "," + user_name + ")"; My_Database.SQLite_Insert(insert_text, connection); string update_text = "update group_table set username = '******' where groupname=" + group_name; My_Database.SQLite_Update(update_text, connection); ListViewItem new_item = new ListViewItem(group_name); } } else { int ind = listView_group.Items.IndexOf(listView_group.SelectedItems[0]); string[] user_list = all_chat_group[ind].user.Split('.'); string send_str = "chat."; string all = null; for (int i = 0; i < user_list.Length; i++) { if (user_list[i] == user_name) { user_list[i] = user_list[0]; user_list[0] = user_name; all = string.Join(".", user_list); break; } } send_str = send_str + all; chat_num = all_chat_group[ind].user.Split('.').Length - 1; all_user.Add(send_str); } //chat_socket = P2P_Communication.Commun_Friend(send_str, client_socket); //Socket chat_socket = P2P_Communication.Commun_Friend(friend_name, client_socket); //创建窗口 Thread thread_chat = new Thread(() => Application.Run(new Chat_Window(all_user, chat_socket, chat_num, 0))); thread_chat.SetApartmentState(System.Threading.ApartmentState.STA);//单线程监听控制 thread_chat.Start(); } }
private void button_register_Click(object sender, EventArgs e) { string username = textBox_username.Text; string password = textBox_password.Text; string password2 = textBox_password2.Text; //密码输入不一致 if (password != password2) { MessageBox.Show(this, "密码输入不一致", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { string info = Server_Connection.Loginto_Server(username, client_socket); if (info == "Incorrect login No.") { MessageBox.Show(this, "用户名不符合规范", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { string search_text = "Select username from user_table where username="******"该用户已被注册", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } //注册成功 else { string insert_text = "insert into user_table(username,password) values(" + username + "," + password + ")"; string insert_text2 = "insert into friend_table(username,friend_list) values(" + username + "," + username + ")"; /* * using (SqlCommand cmd = connection.CreateCommand()) * { * cmd.CommandText = insert_text; * cmd.ExecuteNonQuery(); * cmd.CommandText = insert_text2; * cmd.ExecuteNonQuery(); * } */ My_Database.SQLite_Insert(insert_text, connection); My_Database.SQLite_Insert(insert_text2, connection); MessageBox.Show(this, "注册成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); connection.Close(); this.Close(); } } } textBox_username.Clear(); textBox_password.Clear(); textBox_password2.Clear(); }