Ejemplo n.º 1
0
        /// <summary>
        /// 接收服务端返回消息
        /// </summary>
        private void ReceiveData()
        {
            while (true)
            {
                try
                {
                    var getMsg = clientInstance.br.ReadString();

                    var unPackMsg = TcpHelper.UnPackCommond(getMsg);
                    var commond   = unPackMsg.Item1;
                    switch (commond)
                    {
                    case TcpHelper.TalkCommond.Login:

                        var loginMsg = unPackMsg.Item2.Split(',');
                        if (loginMsg[0].ToLower() == "success")
                        {
                            System.Windows.Application.Current.Dispatcher.Invoke((Action)(() =>
                            {
                                //登录成功
                                OpenWindow(new MainScreenWindow(loginMsg[1]));
                                this.Close();
                            }));
                            return;
                        }
                        else
                        {
                            MessageBox.Show(loginMsg[1]);
                        }


                        break;

                    default:
                        MessageBox.Show("登录失败:" + getMsg);
                        break;
                    }
                }
                catch (Exception e)
                {
                    //MessageBox.Show("接收消息失败:"+e.Message);
                    //关闭连接
                    clientInstance.CloseConnect();
                    return;
                }
            }
        }
Ejemplo n.º 2
0
        private void ReceiveData()
        {
            while (true)
            {
                try
                {
                    var getMsg    = _clientInstance.br.ReadString();
                    var unPackMsg = TcpHelper.UnPackCommond(getMsg);
                    var commond   = unPackMsg.Item1;
                    switch (commond)
                    {
                    case TcpHelper.TalkCommond.Logout:

                        //获取好友列表
                        var sendCommond = TcpHelper.PackCommmond(_userName, TcpHelper.TalkCommond.UpdateUserList);
                        _clientInstance.SendMessage(sendCommond);

                        break;

                    case TcpHelper.TalkCommond.UpdateUserList:
                        try
                        {
                            //获取用户列表
                            var userList = JsonHandler.UnJson <List <TalkUser> >(unPackMsg.Item2);

                            //线程中不能操作UI
                            System.Windows.Application.Current.Dispatcher.Invoke((Action)(() =>
                            {
                                CleanUserList();
                                userList = userList.OrderByDescending(v => v.IsOnline).ToList();
                                foreach (var user in userList)
                                {
                                    AddUserToList(user);
                                }
                            }));
                        }catch (Exception e)
                        {
                            MessageBox.Show(e.Message);
                        }


                        break;

                    case TcpHelper.TalkCommond.Talk:
                        var talkMsgs         = unPackMsg.Item2.Split(',');
                        var talkFromUserName = talkMsgs[0];
                        var talkContent      = unPackMsg.Item2.Substring(talkMsgs[0].Length + 1, unPackMsg.Item2.Length - talkMsgs[0].Length - 1);

                        //将消息存入临时列表
                        StoreTalkMessage(talkFromUserName, talkContent);

                        //判断聊天窗口是否打开
                        TalkWindow tw = IsTalkWindowOpen(talkFromUserName);
                        if (tw == null)
                        {
                            //如果聊天窗口没有打开,则将显示消息条数提示
                            var messageCount = MessageList.memory[talkFromUserName].Count();
                            MessageList.SetMessageTipList(talkFromUserName, messageCount);
                            MessageList.resetMessageTip = true;
                        }

                        break;

                    default:
                        MessageBox.Show(getMsg);
                        break;
                    }
                }catch (Exception e)
                {
                    _clientInstance.CloseConnect();
                    return;
                }
            }
        }