Пример #1
0
        /// <summary>
        /// 双击notifyIcon显示聊天面板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            SingleUtils.isQQTD = false;
            try
            {
                Dictionary <GGUserInfo, MessageInfo> tmpNoReadDic = new Dictionary <GGUserInfo, MessageInfo>(SingleUtils.noReadDic);

                foreach (KeyValuePair <GGUserInfo, MessageInfo> item in tmpNoReadDic)
                {
                    GGUserInfo  fromUser    = item.Key;
                    MessageInfo fromInfo    = item.Value;
                    string      chatFormKey = GGUserUtils.GetChatFormKey(fromUser, SingleUtils.LOGINER);
                    ChatForm    chatFrom    = null;
                    if (SingleUtils.chatForm.ContainsKey(chatFormKey))
                    {
                        chatFrom             = SingleUtils.chatForm[chatFormKey] as ChatForm;
                        chatFrom.WindowState = FormWindowState.Normal;
                    }
                    else
                    {
                        chatFrom = new ChatForm(SingleUtils.LOGINER, fromUser);
                        chatFrom.Show();
                    }
                    chatFrom.showMsgDelMethod(fromInfo);
                }
                SingleUtils.noReadDic.Clear();
            }
            catch (Exception ex)
            {
                SoundUtils.playSound("notifyIcon1_MouseDoubleClick [ 异常:" + ex.Message + " ]", EndPointEnum.PC端);
                //MessageBox.Show("notifyIcon1_MouseDoubleClick [ 异常:" + ex.Message + " ]");
            }
        }
Пример #2
0
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string userId = NickUtils.GetUserId();
                string nick   = this.nick.Text;
                string pwd    = this.pwd.Text;
                string relPwd = this.relPwd.Text;
                string qqSign = this.qqSign.Text;

                string     name   = userId;
                string     extend = Path.GetExtension(fullFileName);
                GGUserInfo user   = new GGUserInfo()
                {
                    createTime = DateTime.Now, qqSign = qqSign, userId = userId, userImg = (headImg == null ? "" : name + extend), userNickName = nick, userPwd = pwd
                };

                if (ChatDBUtils.RegisterUser(user))
                {
                    HeadImgUtils.SaveRegisterUserHeadImg(this.headImg, user);
                    MessageBox.Show(GGUserUtils.ShowNickAndId(user) + "注册成功");
                }
                else
                {
                    MessageBox.Show(GGUserUtils.ShowNickAndId(user) + "注册失败");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Пример #3
0
 /// <summary>
 /// 初始化个人信息
 /// </summary>
 private void InitPerInfo()
 {
     this.picHeadImg.Image = HeadImgUtils.ShowHeadImg(SingleUtils.LOGINER);
     this.lblNickName.Text = GGUserUtils.GetNickAndId(SingleUtils.LOGINER);
     this.lblQQSign.Text   = SingleUtils.LOGINER.qqSign;
     this.Text             = SingleUtils.LOGINER.socket.RemoteEndPoint + "";
 }
Пример #4
0
        /// <summary>
        /// 发送红包
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton9_Click(object sender, EventArgs e)
        {
            this.redBtn.Enabled = false;
            RedOut redOut = new RedOut();

            redOut.Show();
            redOut.sendBtn.Click += (paramSender, paramE) =>
            {
                RedOut tmpRedOut = (paramSender as Button).Parent.Parent as RedOut;

                double money = Double.Parse(redOut.money.Text.Trim());


                toInfo.msgType = MsgType.私发红包;
                toInfo.content = money + "";
                SocketUtils.SendToSingleClient(toInfo);

                toInfo.content = GGUserUtils.ShowNickAndId(SingleUtils.LOGINER) + "给" + GGUserUtils.ShowNickAndId(SingleUtils.toUser) + "发了一个" + money + "元的红包";
                ChatRecordUtils.AppendMsgToClient(this.chatRecords, toInfo);

                tmpRedOut.Close();
                this.redBtn.Enabled = true;
            };
            redOut.button2.Click += (paramSender, paramE) =>
            {
                this.redBtn.Enabled = true;
            };
            int x = this.Left + (this.chatRecords.Width - redOut.Width) / 2;
            int y = this.Top + (this.chatRecords.Height) / 2;

            redOut.Location = new Point(x, y);
        }
Пример #5
0
        public ChatForm(GGUserInfo fromUser, GGUserInfo toUser)
        {
            InitializeComponent();//选择好友私聊
            Control.CheckForIllegalCrossThreadCalls = false;

            this.showMsgDelMethod = this.ShowMsgDelMethodImpl;
            SingleUtils.fromUser  = fromUser;
            SingleUtils.toUser    = toUser;

            toInfo.toId   = toUser.userId;
            toInfo.toUser = SingleUtils.toUser;

            string chatFormKey = GGUserUtils.GetChatFormKey(SingleUtils.fromUser, SingleUtils.toUser);

            SingleUtils.AddChatForm(chatFormKey, this);

            this.Size = new System.Drawing.Size(400, 400);
            this.Tag  = chatFormKey;
            this.Text = "你" + GGUserUtils.ShowNickAndId(SingleUtils.fromUser) + "正在和" + GGUserUtils.ShowNickAndId(SingleUtils.toUser) + "聊天  chatFormKey=" + chatFormKey;

            //表情选择面板
            SingleUtils.emojiForm.VisibleChanged += new EventHandler(this.GetEmoji);
            SingleUtils.emojiForm.Show();
            SingleUtils.emojiForm.WindowState = FormWindowState.Minimized;
            SingleUtils.emojiForm.Visible     = false;
        }
Пример #6
0
        private void button5_Click(object sender, EventArgs e)
        {
            string name = cmbUserList.Text;

            try
            {
                if (OnlineUserUtils.GetAllOnlineClients().Count > 0)
                {
                    string     userId = cmbUserList.SelectedText;
                    GGUserInfo user   = OnlineUserUtils.GetSingleOnlineClient(userId);


                    toInfo.msgType = MsgType.踢出聊天室;
                    toInfo.content = GGUserUtils.ShowNickAndId(user) + "被踢出聊天室";
                    //显示到服务器端
                    ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo);
                    //发送到客户端
                    SocketUtils.SendToMultiClients(OnlineUserUtils.GetAllOnlineClients(), toInfo);
                    //客户端下线
                    OnlineUserUtils.RemoveOnlineClient(user.userId);
                    //刷新客户端
                    this.RefreshOnLineUser();
                }
            }
            catch (Exception ex)
            {
                toInfo.msgType = MsgType.异常报告;
                toInfo.content = "踢人下线时抛出异常:" + ex.Message;
                //显示到服务器端
                ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo);
                //刷新客户端
                this.RefreshOnLineUser();
            }
        }
Пример #7
0
        /// <summary>
        /// 获得好友分组及列表,显示是否在线
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static string GetPerOnlineGroupFriendsStr(string userId)
        {
            List <GGGroup> groupList = ChatDBUtils.GetGroupFriendsInfo(userId);
            string         res       = "";

            foreach (GGGroup group in groupList)
            {
                res += group.groupName + ":[";
                foreach (GGUserInfo user in group.memberList)
                {
                    res += GGUserUtils.GetNickAndId(user);
                    if (onlineUserStr.Contains(user.userId))
                    {
                        res += "[在线]";
                    }
                    else
                    {
                        res += "[离线]";
                    }
                    res += ToolUtils.GetSpaces();
                }
                res += "]";
            }
            return(res);
        }
Пример #8
0
 /// <summary>
 /// 发起震动
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void toolStripButton8_Click(object sender, EventArgs e)
 {
     //发送至服务器
     toInfo.msgType = MsgType.私发抖动;
     SocketUtils.SendToSingleClient(toInfo);
     toInfo.content = "你给" + GGUserUtils.ShowNickAndId(SingleUtils.toUser) + "发了一个抖动";
     ChatRecordUtils.AppendMsgToClient(this.chatRecords, toInfo);
 }
Пример #9
0
        private void UserControl1_Load(object sender, EventArgs e)
        {
            this.width  = 180;
            this.height = 70;
            this.Size   = new Size(this.width, this.height);

            this.head.Image = this.user.headImg;
            this.nick.Text  = GGUserUtils.GetNickAndId(this.user);
            this.btnAdd.Tag = this.user;
        }
Пример #10
0
 private void ChatForm_Shown(object sender, EventArgs e)
 {
     //读取 未读信息
     foreach (KeyValuePair <GGUserInfo, MessageInfo> item in SingleUtils.noReadDic)
     {
         string      chatFormKey = GGUserUtils.GetChatFormKey(item.Key, SingleUtils.LOGINER);
         MessageInfo fromInfo    = item.Value;
         this.ShowMsgDelMethodImpl(fromInfo);
     }
     //清空 未读信息
     SingleUtils.noReadDic.Clear();
 }
Пример #11
0
        /// <summary>
        /// 获得好友列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static string GetPerFriendsStr(string userId)
        {
            List <GGGroup> groupList = ChatDBUtils.GetGroupFriendsInfo(userId);
            string         res       = "";

            foreach (GGGroup group in groupList)
            {
                foreach (GGUserInfo user in group.memberList)
                {
                    res += GGUserUtils.GetNickAndId(user) + ToolUtils.GetSpaces();
                }
            }
            return(res);
        }
Пример #12
0
        private void  除好友ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ListViewItem item = this.friendsList.SelectedItems[0];
            GGUserInfo   user = item.Tag as GGUserInfo;


            DialogResult dr = MessageBox.Show("确认删除" + GGUserUtils.ShowNickAndId(user) + "?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

            if (dr == DialogResult.Yes)
            {
                toInfo.msgType = MsgType.除好友;
                toInfo.toId    = user.userAutoid.ToString();
                toInfo.toUser  = user;
                toInfo.content = user.groupAutoId.ToString();
                SocketUtils.SendToSingleClient(toInfo);
            }
        }
Пример #13
0
 /// <summary>
 /// 委托方法的实现
 /// </summary>
 /// <param name="serverMsg"></param>
 public void ShowMsgDelMethodImpl(MessageInfo fromInfo)
 {
     if (fromInfo.msgType == MsgType.私聊)
     {
         if (fromInfo.fromId != SingleUtils.LOGINER.userId && fromInfo.fromNoRead != 1)
         {
             SoundUtils.NewestInfoCome();
         }
         ChatRecordUtils.AppendMsgToClient(this.chatRecords, fromInfo);
     }
     if (fromInfo.msgType == MsgType.系统消息)
     {
         SoundUtils.SystemSound();
         ChatRecordUtils.AppendMsgToClient(this.chatRecords, fromInfo);
     }
     else if (fromInfo.msgType == MsgType.私发红包)
     {
         Thread thread = new Thread((obj) =>
         {
             MessageInfo tmpFromInfo = obj as MessageInfo;
             int money           = Convert.ToInt32(tmpFromInfo.content);
             SingleUtils.redForm = new RedIn();
             SingleUtils.redForm.pictureBox1.Click += (sender, e) =>
             {
                 string str      = string.Format("收到{0}发来的{1}元的红包", GGUserUtils.ShowNickAndId(tmpFromInfo.fromUser), r.Next(1, 10));
                 toInfo.content  = str;
                 toInfo.dateTime = DateTime.Now;
                 ChatRecordUtils.AppendMsgToClient(this.chatRecords, toInfo);
                 SingleUtils.redForm.Close();
                 this.redBtn.Enabled = true;
             };
             SingleUtils.redForm.Text = "金额=" + money;
             Application.Run(SingleUtils.redForm);
         })
         {
             IsBackground = true
         };
         thread.Start(fromInfo);
     }
     else if (fromInfo.msgType == MsgType.私发抖动)
     {
         ChatRecordUtils.AppendMsgToClient(this.chatRecords, fromInfo);
         WinUtils.WindowShake(this);
     }
 }
Пример #14
0
        /// <summary>
        /// 发送添加好友信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void wuInfo_UserControlBtnAddClicked(object sender, EventArgs e)
        {
            Button     btn  = sender as Button;
            GGUserInfo user = btn.Tag as GGUserInfo;

            if (SingleUtils.LOGINER.userId == user.userId)
            {
                MessageBox.Show("不能添加自己为好友");
                return;
            }
            if (SingleUtils.FriendsStr.Contains(user.userId))
            {
                MessageBox.Show(GGUserUtils.ShowNickAndId(user) + "已经是你的好友了");
                return;
            }
            toInfo.toId   = user.userId;
            toInfo.toUser = user;
            SocketUtils.SendToSingleClient(toInfo);
        }
Пример #15
0
        /// <summary>
        /// 选择好友私聊
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void friendsList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListViewHitTestInfo info = friendsList.HitTest(e.X, e.Y);

            if (info.Item != null)
            {
                GGUserInfo receiveUser = (info.Item as ListViewItem).Tag as GGUserInfo;
                string     chatFormKey = GGUserUtils.GetChatFormKey(SingleUtils.LOGINER, receiveUser);
                if (SingleUtils.chatForm.ContainsKey(chatFormKey))
                {
                    ChatForm cf = SingleUtils.chatForm[chatFormKey] as ChatForm;
                    cf.WindowState = FormWindowState.Normal;
                }
                else
                {
                    new ChatForm(SingleUtils.LOGINER, receiveUser).Show();
                }
            }
        }
Пример #16
0
        private void button12_Click(object sender, EventArgs e)
        {
            string index = cmbUserList.SelectedText;

            if (string.IsNullOrEmpty(index))
            {
                MessageBox.Show("请选择用户");
                return;
            }
            string   path       = textBox2.Text;
            string   extendName = Path.GetExtension(path);
            FileType fileType   = FileUtils.GetFileExtendName(extendName);

            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                foreach (KeyValuePair <string, GGUserInfo> item in OnlineUserUtils.GetAllOnlineClients())
                {
                    byte[]     buffer     = new byte[1024 * 1024 * 5];
                    int        fileLength = fs.Read(buffer, 0, buffer.Length);
                    GGUserInfo user       = OnlineUserUtils.GetSingleOnlineClient(index);

                    toInfo.msgType    = MsgType.私发文件;
                    toInfo.content    = "系统给" + GGUserUtils.ShowNickAndId(user) + "发送了文件";
                    toInfo.fileType   = fileType;
                    toInfo.buffer     = buffer;
                    toInfo.fileLength = fileLength;
                    toInfo.toId       = user.userId;
                    toInfo.toUser     = user;
                    //显示到服务器端
                    ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo);
                    //发送给其他客户端
                    SocketUtils.SendFileToSingleClient(toInfo);
                    //ToolUtils.ServerSendMsgToClients(GGUserInfo.UserSocket1, Convert.ToByte(MsgType.私发文件), "", Convert.ToByte(fileType), buffer, fileLength);
                }
            }
        }
Пример #17
0
        /// <summary>
        /// 服务器获取客户端发来的信息
        /// </summary>
        public void GetClientMsg()
        {
            while (true)
            {
                try
                {
                    if (!this.currentUser.socket.Connected)
                    {
                        toInfo.msgType = MsgType.系统消息;
                        //信息显示到服务器
                        toInfo.content = GGUserUtils.ShowNickAndId(this.currentUser) + " 客户端失去连接";
                        ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo);
                        SoundUtils.playSound(toInfo.content, EndPointEnum.务器);
                        break;
                    }

                    string      json     = ToolUtils.GetString(this.currentUser.socket);
                    MessageInfo fromInfo = SerializerUtil.JsonToObject <MessageInfo>(json);

                    if (fromInfo == null)
                    {
                        //信息显示到服务器
                        MessageInfo tmpInfo = new MessageInfo();
                        tmpInfo.msgType = MsgType.异常报告;
                        tmpInfo.content = GGUserUtils.ShowNickAndId(this.currentUser) + " 未获取到信息";
                        ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, tmpInfo);
                        SoundUtils.playSound(toInfo.content, EndPointEnum.务器);
                        break;
                    }

                    toInfo = new MessageInfo()
                    {
                        msgType  = fromInfo.msgType,
                        socket   = this.currentUser.socket,
                        content  = fromInfo.content,
                        fromId   = fromInfo.fromId,
                        toId     = fromInfo.toId,
                        fromUser = fromInfo.fromUser,
                        toUser   = fromInfo.toUser,
                        dateTime = DateTime.Now
                    };


                    if (!string.IsNullOrEmpty(fromInfo.toId) && OnlineUserUtils.CheckClientIsOnline(fromInfo.toId) && true)
                    {
                        toInfo.socket = OnlineUserUtils.GetSingleOnlineClient(fromInfo.toId).socket;
                    }
                    if (fromInfo.msgType == MsgType.移动好友)
                    {
                        int        oldGroupAutoId = Convert.ToInt32(fromInfo.oldGroupAutoId);
                        int        newGroupAutoId = Convert.ToInt32(fromInfo.newGroupAutoId);
                        int        friendAutoId   = Convert.ToInt32(fromInfo.toId);
                        GGGroup    oldGroupInfo   = ChatDBUtils.GetSingeGroupByAutoId(oldGroupAutoId);
                        GGGroup    newGroupInfo   = ChatDBUtils.GetSingeGroupByAutoId(newGroupAutoId);
                        GGUserInfo userInfo       = ChatDBUtils.GetPerInfoByAutoId(friendAutoId);
                        ChatDBUtils.MoveGroup(oldGroupAutoId, newGroupAutoId, friendAutoId);
                        toInfo.content = "成功将好友" + GGUserUtils.ShowNickAndId(userInfo) + "从[ " + oldGroupInfo.groupName + " ] 移动到 [ " + newGroupInfo.groupName + " ]";
                        SocketUtils.SendToSingleClient(toInfo);
                    }
                    else if (fromInfo.msgType == MsgType.私发红包)
                    {
                        if (OnlineUserUtils.CheckClientIsOnline(fromInfo.toId))
                        {
                            SocketUtils.SendToSingleClient(toInfo);
                        }
                        toInfo.content = GGUserUtils.ShowNickAndId(fromInfo.fromUser) + "给" + GGUserUtils.ShowNickAndId(fromInfo.toUser) + "发了" + fromInfo.content + "元的红包";
                    }
                    else if (fromInfo.msgType == MsgType.私聊)
                    {
                        if (OnlineUserUtils.CheckClientIsOnline(fromInfo.toId))
                        {
                            //信息转发给指定客户端
                            SocketUtils.SendToSingleClient(toInfo);
                            //添加聊天记录
                            ChatDBUtils.AddRecords(fromInfo);
                        }
                        else
                        {
                            //添加离线信息到数据库
                            ChatDBUtils.AddOfflineMsgToClient(fromInfo);
                        }
                    }
                    else if (fromInfo.msgType == MsgType.用户注册)
                    {
                        GGUserInfo user  = SerializerUtil.JsonToObject <GGUserInfo>(json);
                        bool       isSuc = ChatDBUtils.RegisterUser(user);

                        if (isSuc)
                        {
                            toInfo.content = GGUserUtils.ShowNickAndId(user) + "注册成功";
                        }
                        else
                        {
                            toInfo.content = GGUserUtils.ShowNickAndId(user) + "注册失败";
                        }
                        SocketUtils.SendToSingleClient(toInfo);
                    }
                    else if (fromInfo.msgType == MsgType.创建分组)
                    {
                        GGGroup tmpGroup = ChatDBUtils.GetGroupByName(fromInfo.content);
                        if (tmpGroup != null)
                        {
                            toInfo.content = "分组[" + fromInfo.content + "]已存在,请重新命名";
                            SocketUtils.SendToSingleClient(toInfo);
                        }
                        else
                        {
                            ChatDBUtils.CreateGroup(fromInfo.fromUser, fromInfo.content);
                            toInfo.content = "分组[" + fromInfo.content + "]创建成功";
                            SocketUtils.SendToSingleClient(toInfo);
                        }
                    }
                    else if (fromInfo.msgType == MsgType.除分组)
                    {
                        string groupAutoId = fromInfo.content;
                        ChatDBUtils.DelGroup(groupAutoId);
                        toInfo.content = "分组[" + fromInfo.content + "]删除成功";
                        SocketUtils.SendToSingleClient(toInfo);
                    }
                    else if (fromInfo.msgType == MsgType.修改分组)
                    {
                        string[] arr         = fromInfo.content.Split('|');
                        string   groupAutoId = arr[0];
                        string   groupName   = arr[1];
                        ChatDBUtils.UpdateGroup(groupAutoId, groupName);
                        toInfo.content = "分组[" + fromInfo.content + "]修改为[" + groupName + "]";
                        SocketUtils.SendToSingleClient(toInfo);
                    }
                    else if (fromInfo.msgType == MsgType.除好友)
                    {
                        string  friendAutoId = fromInfo.toId;
                        int     groupAutoId  = Convert.ToInt32(fromInfo.content);
                        GGGroup defaultGroup = ChatDBUtils.GetSingeGroupByAutoId(groupAutoId);
                        bool    isSuc        = ChatDBUtils.DelFriend(defaultGroup, friendAutoId);
                        //信息转发给指定客户端
                        toInfo.content = "成功删除好友" + GGUserUtils.ShowNickAndId(fromInfo.toUser);
                        SocketUtils.SendToSingleClient(toInfo);
                    }
                    else if (fromInfo.msgType == MsgType.添加好友)
                    {
                        //检查默认分组
                        CheckDefaultGroup(fromInfo);

                        GGUserInfo user         = ChatDBUtils.GetPerInfoByUserId(fromInfo.toId);
                        GGGroup    defaultGroup = ChatDBUtils.GetDefaultGroup(fromInfo.fromId);
                        bool       isSuc        = ChatDBUtils.AddFriend(defaultGroup, user.userAutoid);
                        //信息转发给指定客户端
                        toInfo.content = "成功添加" + GGUserUtils.ShowNickAndId(fromInfo.toUser) + "为好友";
                        SocketUtils.SendToSingleClient(toInfo);
                    }
                    else if (fromInfo.msgType == MsgType.获取好友信息)
                    {
                        List <GGGroup> groupList = ChatDBUtils.GetGroupFriendsInfo(fromInfo.fromId);
                        toInfo.content = SerializerUtil.ObjectToJson <List <GGGroup> >(groupList);
                        //将好友信息发送给制定客户端,刷新好友在线情况
                        SocketUtils.SendToSingleClient(toInfo);
                        toInfo.content = GGUserUtils.ShowNickAndId(fromInfo.fromUser) + "的好友情况如下:" + ChatDBUtils.GetPerOnlineGroupFriendsStr(fromInfo.fromId);
                    }
                    else if (fromInfo.msgType == MsgType.群发抖动)
                    {
                        //信息分发给其他客户端
                        SocketUtils.SendToMultiClients(OnlineUserUtils.GetAllOnlineClients(), fromInfo);
                    }
                    else if (fromInfo.msgType == MsgType.私发抖动)
                    {
                        toInfo.fromUser = OnlineUserUtils.GetSingleOnlineClient(fromInfo.fromId);
                        if (OnlineUserUtils.CheckClientIsOnline(fromInfo.toId))
                        {
                            toInfo.toUser  = OnlineUserUtils.GetSingleOnlineClient(fromInfo.toId);
                            toInfo.content = GGUserUtils.ShowNickAndId(toInfo.fromUser) + " 给你发了一个抖动";
                            toInfo.socket  = toInfo.toUser.socket;
                            SocketUtils.SendToSingleClient(toInfo);
                        }
                        toInfo.content = GGUserUtils.ShowNickAndId(fromInfo.fromUser) + " 给" + GGUserUtils.ShowNickAndId(fromInfo.toUser) + "发了一个抖动";
                    }
                    else if (fromInfo.msgType == MsgType.退出聊天室)
                    {
                        SocketUtils.SendToMultiClients(OnlineUserUtils.GetAllOnlineClients(), toInfo);
                    }
                    else if (fromInfo.msgType == MsgType.群发红包)
                    {
                        //信息分发给其他客户端
                        SocketUtils.SendToMultiClients(OnlineUserUtils.GetAllOnlineClients(), toInfo);
                    }
                    else if (fromInfo.msgType == MsgType.线)
                    {
                        string     userId = fromInfo.fromId;
                        GGUserInfo user   = OnlineUserUtils.GetSingleOnlineClient(userId);
                        toInfo.content = GGUserUtils.ShowNickAndId(user) + " 下线了";
                        //user.socket.Close();
                        //下线客户端
                        OnlineUserUtils.RemoveOnlineClient(userId);
                    }
                    else if (fromInfo.msgType == MsgType.线)
                    {
                        //信息分发给其他客户端
                        SocketUtils.SendToMultiClients(OnlineUserUtils.GetAllOnlineClients(), toInfo);
                    }
                    else if (fromInfo.msgType == MsgType.群聊)
                    {
                        //信息分发给其他客户端
                        SocketUtils.SendToMultiClients(OnlineUserUtils.GetAllOnlineClients(), toInfo);
                    }
                    else
                    {
                        toInfo.content = "未知信息:[ " + fromInfo.msgType + " ]  " + fromInfo.content;
                    }
                    //信息显示到服务器
                    ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo);
                }
                catch (Exception ex)
                {
                    MessageInfo toInfo = new MessageInfo();
                    toInfo.msgType = MsgType.异常报告;
                    toInfo.content = GGUserUtils.ShowNickAndId(currentUser) + "被强制下线,服务器读取信息时异常:" + ex.Message;
                    //信息显示到服务器
                    ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo);
                    //关闭连接并下线客户端
                    //userInfo.UserSocket1.Close();
                    OnlineUserUtils.RemoveOnlineClient(currentUser.userId);
                    //currentUser.socket.Close();
                    SoundUtils.playSound(toInfo.msgType + toInfo.content, EndPointEnum.务器);
                }
            }
        }
Пример #18
0
        public void GetClientConnection()
        {
            try
            {
                while (true)
                {
                    //1. 服务器接收客户端发来的身份信息
                    //2. 根据发来的信息进行账号验证
                    //3. 反馈给客户端
                    Socket            clientSocket = serverSocket.Accept();
                    string            nameAndPwd   = ToolUtils.GetString(clientSocket);
                    MessageInfo       fromInfo     = SerializerUtil.JsonToObject <MessageInfo>(nameAndPwd);
                    string            sql          = string.Format("select * from GGUser where userId='{0}' and userPwd='{1}'", fromInfo.fromId, fromInfo.fromPwd);
                    List <GGUserInfo> userList     = DBHelper.ConvertToExtModel <GGUserInfo>(sql);

                    toInfo.socket   = clientSocket;
                    toInfo.dateTime = DateTime.Now;
                    if (userList.Count == 1)
                    {
                        GGUserInfo user = userList[0];
                        if (OnlineUserUtils.GetAllOnlineClients().ContainsKey(user.userId))
                        {
                            toInfo.msgType = MsgType.已登录;
                            toInfo.content = GGUserUtils.ShowNickAndId(user) + "已登录";
                            SocketUtils.SendToSingleClient(toInfo);
                        }
                        else
                        {
                            user.socket = clientSocket;
                            //保存用户信息
                            OnlineUserUtils.AddOnlineClient(user.userId, user);
                            //保存在线客户端用户的ids
                            ChatDBUtils.onlineUserStr = OnlineUserUtils.GetOnlineUserStr();
                            //刷新用户
                            RefreshOnLineUser();
                            //服务器反馈客户端
                            toInfo.msgType = MsgType.登陆成功;
                            toInfo.content = SerializerUtil.ObjectToJson <GGUserInfo>(user);
                            SocketUtils.SendToSingleClient(toInfo);
                            //在服务器面板显示记录
                            ServerDelegate temp = msg =>
                            {
                                toInfo.msgType = MsgType.系统消息;
                                toInfo.content = "[ " + msg + "连接到服务器 ]   <--->   " + GGUserUtils.ShowNickAndId(user) + "进入了聊天室";
                                //显示到服务器端
                                ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo);
                            };
                            serverChatRecords.Invoke(temp, clientSocket.RemoteEndPoint);
                            //聊天线程
                            ServerThread myThread = new ServerThread(user, this.serverChatRecords, this.cmbUserList, this.label6);

                            //上线提醒其他客户端
                            toInfo.msgType = MsgType.线;
                            toInfo.fromId  = user.userId;
                            //获取好友列表
                            List <GGGroup>    group      = ChatDBUtils.GetGroupFriendsInfo(user.userId);
                            List <GGUserInfo> friendList = ChatDBUtils.GetFriendsInfo(user.userId);
                            //同时通知自己
                            friendList.Add(user);
                            toInfo.content = ChatDBUtils.GetPerFriendsStr(user.userId);
                            SocketUtils.SendToOnlineFriendClients(OnlineUserUtils.GetAllOnlineClients(), friendList, toInfo);
                            //SocketUtils.SendToMultiClients(OnlineUserUtils.GetAllOnlineClients(), toInfo);

                            toInfo.content = GGUserUtils.ShowNickAndId(user) + "上线,需通知其好友 " + ChatDBUtils.GetPerOnlineFriendsStr(user.userId) + " 刷新在线好友状态";
                            //显示到客户端
                            ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo);
                        }
                    }
                    else if (userList.Count == 0)
                    {
                        toInfo.msgType = MsgType.登陆失败;
                        toInfo.content = "登陆失败,未找到账号";
                        SocketUtils.SendToSingleClient(toInfo);
                    }
                    else
                    {
                        toInfo.msgType = MsgType.登陆失败;
                        toInfo.content = "登陆失败,找到多个账号";
                        SocketUtils.SendToSingleClient(toInfo);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageInfo toInfo = new MessageInfo();
                toInfo.msgType = MsgType.异常报告;
                toInfo.content = "服务端[" + this.GetType() + this.Name + "]接受连接的客户端:" + ex.Message;
                ChatUtils.AppendMsgToServerChatList(this.serverChatRecords, toInfo);
                //MessageBox.Show("服务端[" + this.GetType() + this.Name + "]接受连接的客户端:" + ex.Message);
            }
        }
Пример #19
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;
                }
            }
        }