private void tButCreateGroup_Click(object sender, EventArgs e) { if (frmCreateGroup == null || frmCreateGroup.IsDisposed) { frmCreateGroup = new FormCreateRoom(MyAuth.UserID,MyAuth.UserName, true); frmCreateGroup.CreateRoom += delegate(object senders, exRoom room) { ChangeRoom changeRoom = new ChangeRoom(); changeRoom.type = type.New;//标明是新建群 changeRoom.RoomID = room.RoomID; changeRoom.RoomName = room.RoomName; changeRoom.UserIDs = room.UserIDs; changeRoom.Notice = room.Notice; SendMessageToServer(changeRoom); //发送消息到服务器,创建群组 }; } frmCreateGroup.Show(); frmCreateGroup.Activate(); }
private void onChangeRoom(ChangeRoom changeRoom) { #region 创建临时群 exRoom room = new exRoom(); room.RoomID = changeRoom.RoomID; room.RoomName = changeRoom.RoomName; room.Notice = changeRoom.Notice; room.CreateUserID = changeRoom.CreateUserID; room.UserIDs = changeRoom.UserIDs; if(room.UserIDs !=null) foreach (string userID in room.UserIDs.Split(';')) { User user = findUser(userID); if (user != null) room.Users.Add(user.UserID, user); } #endregion exRoom exroom = findRoom(changeRoom.RoomID);//查找群是否已经存在 if (changeRoom.CreateUserID == MyAuth.UserID)//如果是自己创建的群 { if (changeRoom.type == type.New)//如果创建群成功,更新群成功 { if (this.frmCreateGroup != null && !this.frmCreateGroup.IsDisposed)//如果创建群窗口已经加载 { this.frmCreateGroup.Room = room; this.frmCreateGroup.isUpdateSuccess = true; } if (exroom == null)//如果群不存在,则添加群 { this.Rooms.Add(room); List<exRoom> rooms = new List<exRoom>(); rooms.Add(room); treeViewAddRooms(rooms); } } else if (changeRoom.type == type.set)//如果是自己更新群信息 { if (exroom != null)//如果群存在,则更新群信息即可 { exroom.Notice = room.Notice; exroom.UserIDs = room.UserIDs; exroom.RoomName = room.RoomName; exroom.Users = room.Users; FormTalkRoom frmGroup = exroom.Tag as FormTalkRoom; if (frmGroup != null) frmGroup.Room = exroom; OurMsg.FormCreateRoom frm = (exroom.FormData as FormCreateRoom); if (frm != null && !frm.IsDisposed) { frm.Room = room; frm.isUpdateSuccess = true; } } } } else//如果群信息发生改变 { if (changeRoom.type == type.New || changeRoom.type == type.set)//如果加入群 { if (exroom == null)//如果群不存在,则添加群 { this.Rooms.Add(room); List<exRoom> rooms = new List<exRoom>(); rooms.Add(room); treeViewAddRooms(rooms); } else//如果群存在,则更新信息 { exroom.Notice = room.Notice; exroom.UserIDs = room.UserIDs; exroom.RoomName = room.RoomName; exroom.Users = room.Users; FormTalkRoom frmGroup = exroom.Tag as FormTalkRoom; if (frmGroup != null) frmGroup.Room = exroom; } } else if (changeRoom.type == type.delete)//如果退出群 { if (exroom != null) { Form frm = exroom.Tag as Form; if (frm != null && !frm.IsDisposed) frm.Close(); frm = exroom.FormData as Form; if (frm != null && !frm.IsDisposed) frm.Close(); TreeNode node = (exroom.TreeNode as TreeNode); if (node != null) node.Remove(); this.Rooms.Remove(exroom); } } } }
/// <summary> /// 获得群对话框 /// </summary> /// <param name="room"></param> private FormTalkRoom GetRoomMsgForm(exRoom room) { FormTalkRoom fs = null; try { fs = room.Tag as FormTalkRoom; if (fs == null || fs.IsDisposed) { fs = new FormTalkRoom();//发送消息对话框根据需要替换 room.Tag = fs; ///群中创建用户对话窗口事件发生时 fs.CreateFormTalkUser += delegate(object sender, exUser user) { FormTalkUser fTalkUser = GetUserMsgForm(user);// 获得用户消息对话框 fTalkUser.Show(); fTalkUser.Activate(); }; //更新群事件发生时 fs.UpdateRoom += delegate(object sender, exRoom updateRoom) { ChangeRoom changeRoom = new ChangeRoom(); changeRoom.type = type.set; changeRoom.RoomID = updateRoom.RoomID; changeRoom.RoomName = updateRoom.RoomName; changeRoom.UserIDs = updateRoom.UserIDs; changeRoom.Notice = updateRoom.Notice; SendMessageToServer(changeRoom); //向服务器请求更新群组资料 }; ///当在群里发送消息事件发生时 fs.SendMsgToGroup += delegate(IMLibrary3.Protocol.Element e, exRoom Room) { SendMessageToServer(e);//发送消息到服务器 if (e is IMLibrary3.Protocol.Message) OpeRecordDB.AddMsg(e as IMLibrary3.Protocol.Message);//将消息添加到数据库 }; fs.FormClosed += delegate(object sender, FormClosedEventArgs e) { fs.Dispose(); fs = null; room.Tag = null; }; fs.Room = room; fs.myUserID = MyAuth.UserID ; fs.myUserName = MyAuth.UserName; } } catch (Exception ex) { IMLibrary3.Global.MsgShow(ex.Message); } return fs; }
/// <summary> /// 处理用户请求创建或更新群 /// </summary> /// <param name="changeRoom"></param> /// <param name="session"></param> private void onChangeRoom(ChangeRoom changeRoom, TCPServerSession session) { UserVcard requestUser = session.Tag as UserVcard;//获得请求用户 if (changeRoom.type == type.New && requestUser.CreatedRoomsCount < requestUser.CreateRooms)//如果请求创建群 { #region 创建群 //10次随机产生群号(所谓摇号),如果10次摇出的群号都已被创建,则退出并表示服务器忙 //10都不能摇到空号表示用户运气不好,下次再试! for (int i = 0; i < 10; i++) { Random ran = new Random(); int RandKey = ran.Next(10000, 1000000000);//开始摇号,群号码从10000至1000000000随机产生 if (!Rooms.ContainsKey(RandKey.ToString()))//如果是空号,以此号创建群 { Room room = new Room(); room.RoomID = RandKey.ToString(); room.RoomName = changeRoom.RoomName; room.Notice = changeRoom.Notice; room.UserIDs = changeRoom.UserIDs; room.CreateUserID = changeRoom.from;//创建者为请求者 requestUser.CreatedRoomsCount++;//标记用户创建群数 #region 将内存中的用户添加到内存中的群 string[] userids = room.UserIDs.Split(';'); foreach (string userID in userids) { UserVcard user = getUser(userID); if (user != null) { if (room.Users == null) room.Users = new Dictionary<string, User>(); if (!room.Users.ContainsKey(userID)) room.Users.Add(user.UserID, user); if (user.Rooms == null) user.Rooms = new Dictionary<string, Room>(); if (!user.Rooms.ContainsKey(room.RoomID)) user.Rooms.Add(room.RoomID, room); } } room.UserIDs = "";//重新生成群包含的Users foreach (User u in GetUsers(room)) room.UserIDs += u.UserID + ";"; #endregion Rooms.Add(room.RoomID, room);//将创建的群添加到内存 DBHelper.CreateRoom(room);//将创建的群添加到数据库 changeRoom.RoomID = room.RoomID; changeRoom.CreateUserID = room.CreateUserID; SendMessageToRoom(room.CreateUserID, room, Factory.CreateXMLMsg(changeRoom));//发送消息到刚创建的群,通知群里的用户已经加入群 return;//创建群成功,退出 } } #endregion } else if (changeRoom.type == type.set)//请求更新群 { #region 更新群 Room room = getRoom(changeRoom.RoomID); if (room != null && room.CreateUserID == changeRoom.from)//如果群创建者为请求用户 { string oldVersion = room.RoomName.Trim() + room.Notice.Trim() + room.UserIDs; changeRoom.CreateUserID = room.CreateUserID; room.Notice = changeRoom.Notice; room.RoomName = changeRoom.RoomName; string[] newUserids = changeRoom.UserIDs.Split(';'); #region 查找群中被删除的用户 changeRoom.type = type.delete;//标识删除群 changeRoom.UserIDs = null;//不要将新群包含的用户通知退出群的用户 string delRoomMsg = IMLibrary3.Protocol.Factory.CreateXMLMsg(changeRoom); bool t = false; foreach (UserVcard user in GetUsers(room)) { t = false; foreach (string userID in newUserids) if (user.UserID == userID)//如果群里的用户在更新后的用户集合中存在,则表示未删除 t = true; if (!t)//如果更新的用户集合中没有当前用户,则表示从群中删除此用户 { room.Users.Remove(user.UserID); user.Rooms.Remove(room.RoomID); SendMessageToUser(user, delRoomMsg);//通知用户退出群 } } #endregion #region 添加新用户记录群中新用户 foreach (string userID in newUserids) { UserVcard user = getUser(userID);//获取新用户 if (user != null) { if (!room.Users.ContainsKey(user.UserID)) { room.Users.Add(user.UserID, user);//如果群里无此用户,则新增用户 if (!user.Rooms.ContainsKey(room.RoomID)) user.Rooms.Add(room.RoomID, room);//如果用户不在此群,则为用户新增群 } } } #endregion room.UserIDs = "";//重新生成群包含的Users foreach (User u in GetUsers(room)) room.UserIDs += u.UserID + ";"; changeRoom.type = type.set;//标识群信息被成功修改 changeRoom.UserIDs = room.UserIDs;//设置最新用户列表 string newVersion = room.RoomName.Trim() + room.Notice.Trim() + room.UserIDs; if (oldVersion == newVersion) return;//如果没有做任何更改 SendMessageToRoom(room.CreateUserID, room, IMLibrary3.Protocol.Factory.CreateXMLMsg(changeRoom));//通知群内原用户,群信息已经修改 DBHelper.UpdateRoom(room); } #endregion } }