Exemplo n.º 1
0
        private void Chat_Click(object sender, RoutedEventArgs e)
        {
            Button  button = (Button)sender;
            User    user2  = null;
            UserBLL bll    = new UserBLL();

            bll.GetUserByUsername(button.Name, out user2);
            if (user2 == null)
            {
                MessageBox.Show("此用户已被删除!");
                return;
            }
            OneToOne one = new OneToOne(user, user2, clientSocket);

            App.list.Add(one);
            one.Show();
        }
Exemplo n.º 2
0
        public void ReceiveMessage(object clientSocket)
        {
            Socket connection = (Socket)clientSocket;

            while (true)
            {
                try
                {
                    //接受数据
                    byte[] result = new byte[1024];
                    //通过clientSocket接收数据
                    int receiveNumber = connection.Receive(result);
                    //把接受的数据从字节类型转化为字符类型
                    String recStr = Encoding.UTF8.GetString(result, 0, receiveNumber);
                    //[0]发送者ID [1]消息内容 [2]消息类型
                    string[] StrArr = System.Text.RegularExpressions.Regex.Split(recStr, "###");
                    if (recStr != string.Empty)
                    {
                        if (StrArr[2] != "4" && StrArr[2] != "5" && StrArr[2] != "6")
                        {
                            User chatuser = new User();
                            userbll.GetUserByID(Convert.ToInt32(StrArr[0]), out chatuser);
                            //MessageBox.Show("收到来自" + chatuser.Name + "的新消息");
                            this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                                                        (ThreadStart) delegate()
                            {
                                RefreshUser();
                                int i;
                                for (i = 0; i < App.list.Count; i++)
                                {
                                    OneToOne o = App.list[i];
                                    if (o.msgreceiver.Name == chatuser.Name)
                                    {
                                        if (StrArr[2] == "3")
                                        {
                                            o.n = 0;
                                            o.RefreshMessage();
                                        }
                                        else
                                        {
                                            string msg = "\n" + chatuser.Name + "    " + DateTime.Now.ToString() + "\n" + StrArr[1] + "\n";
                                            o.ShowMessage.AppendText(msg);
                                            o.ShowMessage.ScrollToEnd();
                                            o.msgscroll.ScrollToEnd();
                                        }

                                        break;
                                    }
                                }
                                if (i >= App.list.Count)
                                {
                                    OneToOne one = new OneToOne(user, chatuser, connection);
                                    App.list.Add(one);
                                    one.Show();
                                }
                            });
                        }
                        else if (StrArr[3] != user.Id.ToString())
                        {
                            GroupBLL     groupBLL = new GroupBLL();
                            List <Group> groups   = new List <Group>();
                            groups = groupBLL.GetGroupListByUserId(user.Id);
                            foreach (Group g in groups)
                            {
                                if (g.Id == Convert.ToInt32(StrArr[0]))
                                {
                                    //MessageBox.Show("收到来自" + g.Name + "的新消息");
                                    this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                                                                (ThreadStart) delegate()
                                    {
                                        RefreshUser();
                                        RefreshGroup();
                                        int i;
                                        for (i = 0; i < App.list2.Count; i++)
                                        {
                                            OneToManyWindow o = App.list2[i];
                                            if (o.group.Id == g.Id)
                                            {
                                                if (StrArr[2] == "6")
                                                {
                                                    o.n = 0;
                                                    o.RefreshGroupMessage();
                                                }
                                                else
                                                {
                                                    UserBLL userBLL = new UserBLL();
                                                    User temp       = null;
                                                    userBLL.GetUserByID(Convert.ToInt32(StrArr[3]), out temp);
                                                    string msg = "\n" + temp.Name + "    " + DateTime.Now.ToString() + "\n" + StrArr[1] + "\n";
                                                    o.ShowMessage.AppendText(msg);
                                                    o.ShowMessage.ScrollToEnd();
                                                    o.msgscroll.ScrollToEnd();
                                                }
                                                break;
                                            }
                                        }
                                        if (i >= App.list2.Count)
                                        {
                                            OneToManyWindow many = new OneToManyWindow(g, user);
                                            App.list2.Add(many);
                                            many.Show();
                                        }
                                    });
                                    break;
                                }
                            }
                        }
                        ConnectToServer();
                    }

                    //    text2.Dispatcher.BeginInvoke(

                    //           new Action(() => { text2.Text += "\r\n" + recStr; }), null);

                    //
                }

                catch (Exception ex)
                {
                    //connection.Shutdown(SocketShutdown.Both);
                    //connection.Close();
                    break;
                }
            }
        }