コード例 #1
0
        private void RefreshGroup()
        {
            for (int i = GroupList.Items.Count - 1; i > 0; i--)
            {
                GroupList.Items.RemoveAt(i);
            }
            GroupBLL groupbll = new GroupBLL();

            GroupsList = groupbll.GetGroupListByUserId(user.Id);
            foreach (Group p in GroupsList)
            {
                Image groupimage = new Image();
                groupimage.Source = new BitmapImage(new Uri("images\\214743981.jpg", UriKind.Relative));
                Label      grouplabel = new Label();
                StackPanel groupstack = new StackPanel();
                groupimage.MaxHeight = 50;
                groupimage.MaxWidth  = 50;
                grouplabel.VerticalContentAlignment = VerticalAlignment.Center;
                grouplabel.Content     = p.Name;
                groupstack.Orientation = Orientation.Horizontal;
                groupstack.Children.Add(groupimage);
                groupstack.Children.Add(grouplabel);
                Button groupbutton = new Button();
                groupbutton.Background        = Brushes.White;
                groupbutton.Content           = groupstack;
                groupbutton.Margin            = new Thickness(10);
                groupbutton.Name              = "no_" + p.Id.ToString();
                groupbutton.BorderThickness   = new Thickness(0);
                groupbutton.MouseDoubleClick += GroupChat_Click;
                GroupList.Items.Add(groupbutton);
            }
        }
コード例 #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;
                }
            }
        }