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); } }
/// <summary> /// 获取未读信息 /// </summary> private void GetNoReadMsg() { string sql = string.Format("SELECT * FROM [dbo].[{0}] where receiveId='{1}' and isRead=0 ", DBTUtils.DBT_NoReadMsg, SingleUtils.LOGINER.userId); DBHelper.Excute(string.Format("update [dbo].[{0}] set isRead=1 where receiveId='{1}' and isRead=0 ", DBTUtils.DBT_NoReadMsg, SingleUtils.LOGINER.userId)); List <NoReadMsg> list = DBHelper.ConvertToModel <NoReadMsg>(sql); if (list.Count > 0) { SoundUtils.NewestInfoCome(); SingleUtils.isQQTD = true; } foreach (NoReadMsg msg in list) { GGUserInfo fromUser = ChatDBUtils.GetPerInfoByUserId(msg.sendId); MessageInfo fromInfo = new MessageInfo(); fromInfo.fromId = msg.sendId; fromInfo.toId = msg.receiveId; fromInfo.content = msg.content; fromInfo.msgType = msg.msgType; fromInfo.dateTime = msg.datetime; fromInfo.fromNoRead = 1; SingleUtils.noReadDic.Add(fromUser, fromInfo); //添加聊天记录 ChatDBUtils.AddRecords(fromInfo); } }
/// <summary> /// 检查是否存在默认分组,不存在则添加 /// </summary> /// <param name="fromInfo"></param> private static void CheckDefaultGroup(MessageInfo fromInfo) { GGUserInfo tmpUser = ChatDBUtils.GetPerInfoByUserId(fromInfo.fromId); GGGroup defaultGroup = ChatDBUtils.GetDefaultGroup(tmpUser.userId); if (defaultGroup == null) { ChatDBUtils.CreateGroup(tmpUser, "我的好友", true); } }
private void button1_Click(object sender, EventArgs e) { if (true) { List <GGUserInfo> user = ChatDBUtils.GetAllUser(); foreach (GGUserInfo item in user) { HeadImgUtils.SaveRegisterUserHeadImg(Image.FromFile(""), item); } MessageBox.Show("完成"); } }
/// <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.务器); } } }
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); } }