示例#1
0
        /// <summary>
        ///  客户端接收服务器发来的消息
        /// </summary>
        public void GetServerMsg()
        {
            while (true)
            {
                try
                {
                    int         msgLength;
                    byte[]      buffer   = ToolUtils.GetByte(SingleUtils.LOGINER.socket, out msgLength);
                    string      json     = Encoding.UTF8.GetString(buffer, 0, msgLength);
                    MessageInfo fromInfo = null;
                    byte[]      fileInfo = null;
                    try
                    {
                        fromInfo = SerializerUtil.JsonToObject <MessageInfo>(json);
                    }
                    catch (Exception ex)
                    {
                        fileInfo = Encoding.UTF8.GetBytes(json);
                    }
                    //接收文件
                    if (fileInfo != null)
                    {
                        MsgType  msgType  = (MsgType)fileInfo[0];
                        FileType fileType = (FileType)fileInfo[1];
                        SingleUtils.fileByteList.Add(new MessageInfo()
                        {
                            content = ToolUtils.GetFilePath() + DateTime.Now.ToString("yyyyMMddHHmmssfff") + "." + fileType, fileLength = msgLength, msgType = msgType, fileType = fileType, buffer = fileInfo
                        });
                        SingleUtils.isWPTD = true;
                    }
                    else
                    {
                        if (fromInfo.msgType == MsgType.创建分组 || fromInfo.msgType == MsgType.移动好友 || fromInfo.msgType == MsgType.除好友 || fromInfo.msgType == MsgType.添加好友)
                        {
                            MessageBox.Show(fromInfo.content);
                            toInfo.msgType = MsgType.获取好友信息;
                            SocketUtils.SendToSingleClient(toInfo);
                        }
                        else if (fromInfo.msgType == MsgType.用户注册)
                        {
                            MessageBox.Show(fromInfo.content);
                        }
                        else if (fromInfo.msgType == MsgType.获取好友信息)
                        {
                            List <GGGroup> group            = SerializerUtil.JsonToObject <List <GGGroup> >(fromInfo.content);
                            Thread         loadFriendThread = new Thread(this.LoadFriends)
                            {
                                IsBackground = true
                            };
                            loadFriendThread.Start(group);
                        }
                        else if (fromInfo.msgType == MsgType.线 || fromInfo.msgType == MsgType.线)
                        {
                            if (fromInfo.msgType == MsgType.线 && fromInfo.fromId != SingleUtils.LOGINER.userId)
                            {
                                SoundUtils.SystemSound();
                            }
                            SingleUtils.FriendsStr = fromInfo.content;
                            // 1.客户端收到 有人上线 指令
                            // 2.向服务器发送[ 获取好友信息 ] 的请求
                            // 3.服务器向客户端反馈[ 获取好友信息 ] 的响应后,调用 this.LoadFriends(group);方法刷新好友数据
                            toInfo.msgType = MsgType.获取好友信息;
                            SocketUtils.SendToSingleClient(toInfo);
                        }
                        if (fromInfo.msgType == MsgType.私发文件 || fromInfo.msgType == MsgType.群发文件)
                        {
                            MessageBox.Show(fromInfo.msgType + "");
                        }
                        //将信息展现到 聊天面板
                        else if (fromInfo.msgType == MsgType.私聊 || fromInfo.msgType == MsgType.私发抖动 || fromInfo.msgType == MsgType.私发红包 || fromInfo.msgType == MsgType.系统消息)
                        {
                            GGUserInfo fromUser = fromInfo.fromUser;
                            GGUserInfo toUser   = fromInfo.toUser;

                            //发送者和接收者反转显示
                            string chatFormKey = GGUserUtils.GetChatFormKey(toUser, fromUser);

                            //存在聊天窗口
                            if (SingleUtils.chatForm.ContainsKey(chatFormKey))
                            {
                                ChatForm chatFrom = SingleUtils.chatForm[chatFormKey] as ChatForm;
                                if (chatFrom.showMsgDelMethod != null)
                                {
                                    chatFrom.WindowState = FormWindowState.Normal;
                                    chatFrom.showMsgDelMethod(fromInfo);
                                }
                            }
                            //不存在聊天窗口
                            else
                            {
                                SoundUtils.NewestInfoCome();
                                //将信息添加到集合里面
                                fromInfo.fromNoRead = 1;
                                SingleUtils.noReadDic.Add(fromUser, fromInfo);
                                if (fromInfo.msgType == MsgType.私发抖动 || fromInfo.msgType == MsgType.私发红包)
                                {
                                    Thread openChatWinThread = new Thread((obj) =>
                                    {
                                        MessageInfo tmpFromInfo = obj as MessageInfo;
                                        ChatForm chatForm       = new ChatForm(tmpFromInfo.toUser, tmpFromInfo.fromUser);
                                        Application.Run(chatForm);
                                    })
                                    {
                                        IsBackground = false
                                    };
                                    openChatWinThread.Start(fromInfo);
                                }
                                else
                                {
                                    SingleUtils.isQQTD = true;
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageInfo toInfo = new MessageInfo();
                    toInfo.msgType = MsgType.异常报告;
                    toInfo.content = "客户端接收服务器发来的消息报错   ex=" + ex.Message;
                    //this.showMsgDelMethod(toInfo);
                    iconHitTimer.Enabled = true;
                    iconHitTimer.Start();
                    MessageBox.Show("[" + SingleUtils.LOGINER.userNickName + "]在首页客户端 [ " + this.GetType() + this.Name + " ] 接收消息时报错:" + ex.Message);
                    SoundUtils.playSound(toInfo.msgType + toInfo.content, EndPointEnum.PC端);
                    break;
                }
            }
        }