//通过好友申请 private void button_approve_Click(object sender, EventArgs e) { string friend_name = label2.Text.Split(' ')[0]; Socket add_friend_socket = P2P_Communication.Commun_Friend(friend_name, client_socket); string send_msg = "aa." + user_name; P2P_Communication.Connect_Send(add_friend_socket, send_msg); Thread.Sleep(10); add_friend_socket.Shutdown(SocketShutdown.Both); add_friend_socket.Close(); label1.Hide(); label2.Hide(); button_approve.Hide(); button_refuse.Hide(); button_refuse.Enabled = false; button_approve.Enabled = false; if (friend_list == null) { friend_list = friend_name; } else { friend_list = friend_list + "." + friend_name; } string update_text = "update friend_table set friend_list = '" + user_name + "." + friend_list + "' where username="******"online"); listView1.Items.Add(new_item); }
//更改群名 private void textBox_name_TextChanged(object sender, EventArgs e) { string current_group_name = findGroup(user_name); string new_group_name = textBox_name.Text; string update_text = "Update group_table set groupname='" + new_group_name + "' where groupname = '" + current_group_name + "'"; My_Database.SQLite_Update(update_text, connection); }
//发起聊天 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(); } }