コード例 #1
0
        /// <summary>
        ///     接收消息
        /// </summary>
        private void ReceiveMsg()
        {
            while (true)
            {
                // 定义一个缓冲区保存接收的消息
                byte[] arrMsg = new byte[1024 * 1024 * 2];

                // 保存数据长度
                int length = -1;

                try
                {
                    // 获取信息
                    length = socketClient.Receive(arrMsg);

                    // 解析字符串
                    string msgReceive = Encoding.UTF8.GetString(arrMsg, 0, length);

                    // 消息
                    if (arrMsg[0] == IS_LOGIN)
                    {
                        userData = (UserData)JsonConvert.DeserializeObject(msgReceive.Substring(1, msgReceive.Length - 1), typeof(UserData));

                        Application.Current.Dispatcher.Invoke(new Action(delegate { LoginSuccess(); }));

                    }
                    else if (arrMsg[0] == IS_NOT_LOGIN)
                    {
                        //Application.Current.Dispatcher.Invoke(new Action(delegate { AddText(); }));

                        this.hint.Dispatcher.Invoke(new Action(() =>
                        {
                            this.hint.Content = "输入的账号或密码有错误!";
                        }));
                        this.accountTextBox.Dispatcher.Invoke(new Action(() =>
                        {
                            this.accountTextBox.Text = "";
                        }));
                        this.passwordTextBox.Dispatcher.Invoke(new Action(() =>
                        {
                            this.passwordTextBox.Password = "";
                        }));
                    }
                    else if (arrMsg[0] == IS_REGISTER)
                    {
                        UserData userdata = (UserData)JsonConvert.DeserializeObject(msgReceive.Substring(1, msgReceive.Length - 1), typeof(UserData));

                        Application.Current.Dispatcher.Invoke(new Action(delegate { RegisterSuccess(userdata); }));
                    }
                    else if (arrMsg[0] == IS_NOT_REGISTER)
                    {
                        MessageBox.Show("注册失败!");
                    }
                    else if (arrMsg[0] == IS_UPDATE)
                    {
                        userData = (UserData)JsonConvert.DeserializeObject(msgReceive.Substring(1, msgReceive.Length - 1), typeof(UserData));

                        Application.Current.Dispatcher.Invoke(new Action(delegate { UpdateInfoSuccess(); }));
                    }
                    else if (arrMsg[0] == IS_NOT_UPDATE)
                    {
                        MessageBox.Show("更新信息失败!");
                    }
                    else if (arrMsg[0] == IS_EDIT_FRIEND)
                    {
                        Application.Current.Dispatcher.Invoke(new Action(delegate
                        {
                           if (addFriendFlag && !subFriendFlag)
                                MessageBox.Show("添加好友成功!");
                           else
                                MessageBox.Show("删除好友成功!");
                        }));

                    }
                    else if (arrMsg[0] == IS_NOT_EDIT_FRIEND)
                    {
                        Application.Current.Dispatcher.Invoke(new Action(delegate
                        {
                            if (addFriendFlag && !subFriendFlag)
                                MessageBox.Show("添加好友失败!");
                            else
                                MessageBox.Show("删除好友失败!");
                        }));
                    }
                    else if (arrMsg[0] == UPDATE_FRIENDLIST)
                    {
                        FriendlistHandler friendlistHandler = (FriendlistHandler)JsonConvert.DeserializeObject(msgReceive.Substring(1, msgReceive.Length - 1), typeof(FriendlistHandler));
                        if (friendlistHandler.type == ADD_ONLINE_FRIEND)
                        {
                            bool flag = true;
                            foreach (onlineFriendType oFT in onlineFriendlist)
                            {
                                if (oFT.userData.userId != null && oFT.userData.userId.Equals(friendlistHandler.friendInfo.userId))
                                {
                                    flag = false;
                                }
                            }
                            if (flag)
                            {
                                onlineFriendType newFriend = new onlineFriendType(friendlistHandler.friendInfo);
                                try
                                {
                                    Application.Current.Dispatcher.Invoke(new Action(delegate
                                    {
                                        // 新增一条在线好友信息
                                        onlineFriendlist.Add(newFriend);
                                        // 为新上线的好友分配一个聊天框
                                        List<Paragraph> paragraph = new List<Paragraph>();
                                        dictParagraph.Add(newFriend.userData.userId, paragraph);

                                        IntPtr activeForm = GetActiveWindow(); // 先得到当前的活动窗体
                                        Prompt test = new Prompt(newFriend.userData.userName+"("+newFriend.userData.userId+")"+" 已上线", 0);
                                        cnt = (cnt + 1) % 6;
                                        test.Show();
                                        SetActiveWindow(activeForm); // 在把焦点还给之前的活动窗体
                                    }));
                                }
                                catch (Exception e)
                                {
                                    MessageBox.Show(e.Message);
                                }
                            }
                        }
                        else if (friendlistHandler.type == REMOVE_ONLINE_FRIEND)
                        {

                            foreach (onlineFriendType oFT in onlineFriendlist)
                            {
                                if (oFT.userData.userId != null && oFT.userData.userId.Equals(friendlistHandler.friendInfo.userId))
                                {
                                    try
                                    {
                                        Application.Current.Dispatcher.Invoke(new Action(delegate
                                        {
                                            onlineFriendType sendTo = (onlineFriendType)onlineFriendlistListBox.SelectedItem;
                                            if (sendTo.nameAndId.Equals("所有人") == false && sendTo.userData.userId.Equals(oFT.userData.userId))
                                            {
                                                onlineFriendlistListBox.SelectedIndex = 0;

                                                chatRichTextBox.Document.Blocks.Clear();
                                                foreach (Paragraph p in dictParagraph["AllUsers"])
                                                    chatRichTextBox.Document.Blocks.Add(p);
                                            }

                                            IntPtr activeForm = GetActiveWindow(); // 先得到当前的活动窗体
                                            Prompt test = new Prompt(oFT.userData.userName + "(" + oFT.userData.userId + ")" + " 已下线", 0);
                                            cnt = (cnt + 1) % 6;
                                            test.Show();
                                            SetActiveWindow(activeForm); // 在把焦点还给之前的活动窗体

                                            // 将下线好友的聊天框移除
                                            dictParagraph.Remove(oFT.userData.userId);
                                            // 将下线好友从在线好友列表中移除
                                            onlineFriendlist.Remove(oFT);

                                        }));
                                        break;
                                    }
                                    catch (Exception e)
                                    {
                                        MessageBox.Show(e.Message);
                                    }
                                }
                            }
                        }
                    }
                    else if (arrMsg[0] == MSG_TO_ALL)
                    {
                        MsgHandler msgHandler = (MsgHandler)JsonConvert.DeserializeObject(msgReceive.Substring(1, msgReceive.Length - 1), typeof(MsgHandler));

                        Application.Current.Dispatcher.Invoke(new Action(delegate { ReceiveMsgForAll(msgHandler); }));
                    }
                    else if (arrMsg[0] == MSG_TO_INDIVIDULAL)
                    {
                        MsgHandler msgHandler = (MsgHandler)JsonConvert.DeserializeObject(msgReceive.Substring(1, msgReceive.Length - 1), typeof(MsgHandler));

                        Application.Current.Dispatcher.Invoke(new Action(delegate { ReceiveMsgForIndividual(msgHandler); }));
                    }
                    else if (arrMsg[0] == FILE_TO_ALL)
                    {
                        FileHandler msgHandler = (FileHandler)JsonConvert.DeserializeObject(msgReceive.Substring(1, msgReceive.Length - 1), typeof(FileHandler));

                        Application.Current.Dispatcher.Invoke(new Action(delegate { ReceiveFileForAll(msgHandler); }));
                    }
                    else if (arrMsg[0] == FILE_TO_INDIVIDUAL)
                    {
                        FileHandler msgHandler = (FileHandler)JsonConvert.DeserializeObject(msgReceive.Substring(1, msgReceive.Length - 1), typeof(FileHandler));

                        Application.Current.Dispatcher.Invoke(new Action(delegate { ReceiveFileForIndividual(msgHandler); }));
                    }
                    else
                    {
                        MessageBox.Show(Encoding.UTF8.GetString(arrMsg));
                    }
                }
                catch (SocketException se)
                {
                    //MessageBox.Show("【错误】接收消息异常:" + se.Message);
                    return;
                }
                catch (Exception e)
                {
                    //MessageBox.Show("【错误】接收消息异常:" + e.Message);
                    return;
                }
            }
        }
コード例 #2
0
 public FriendlistHandler(int t, UserData f)
 {
     type = t; friendInfo = f;
 }
コード例 #3
0
 public onlineFriendType(UserData ud)
 {
     nameAndId = ud.userName + "(" + ud.userId + ")";
     msgNum = 0;
     msgNumString = "";
     userData = ud;
 }
コード例 #4
0
 public onlineFriendType(string nAI)
 {
     nameAndId = nAI;
     msgNum = 0;
     msgNumString = "";
     userData = new UserData();
 }
コード例 #5
0
        private void RegisterSuccess(UserData userdata)
        {
            MessageBox.Show("注册成功!\r\n您的账号为:" + userdata.userId + "\r\n您的密码为:" + userPasswordRegister.Text);
            userNameRegister.Text = "";
            userPasswordRegister.Text = "";
            userGenderRegister.Text = "";
            userAgeRegister.Text = "";

            accountTextBox.Text = "";
            passwordTextBox.Password = "";

            userRegisterCanvas.Visibility = Visibility.Hidden;
            userLoginCanvas.Visibility = Visibility.Visible;
        }