Esempio n. 1
0
        /// <summary>
        ///  更新群
        /// </summary>
        /// <param name="room">群</param>
        public static bool UpdateRoom(IMLibrary3.Organization.Room room)
        {
            try
            {
                string sql = "update  Rooms set RoomName=@RoomName,Notice=@Notice,Users=@Users"
                             + "  where (RoomID=@RoomID)";

                System.Data.SqlClient.SqlParameter[] sqlpar = new System.Data.SqlClient.SqlParameter[]
                { new System.Data.SqlClient.SqlParameter("@RoomID", room.RoomID),
                  new System.Data.SqlClient.SqlParameter("@RoomName", room.RoomName),
                  new System.Data.SqlClient.SqlParameter("@Notice", room.Notice),
                  new System.Data.SqlClient.SqlParameter("@Users", room.UserIDs), };
                IMLibrary.SqlData.DataAccess.ExecSql(sql, sqlpar);
                return(true);
            }
            catch { return(false); }
        }
Esempio n. 2
0
        /// <summary>
        /// 创建群
        /// </summary>
        /// <param name="room">群</param>
        public static bool CreateRoom(IMLibrary3.Organization.Room room)
        {
            try
            {
                string sql = "insert into Rooms(RoomID,RoomName,Notice,Users,CreateUserID,CreateDateTime)"
                             + " values(@RoomID,@RoomName,@Notice,@Users,@CreateUserID,@CreateDateTime)";

                System.Data.SqlClient.SqlParameter[] sqlpar = new System.Data.SqlClient.SqlParameter[]
                { new System.Data.SqlClient.SqlParameter("@RoomID", room.RoomID),
                  new System.Data.SqlClient.SqlParameter("@RoomName", room.RoomName),
                  new System.Data.SqlClient.SqlParameter("@Notice", room.Notice),
                  new System.Data.SqlClient.SqlParameter("@Users", room.UserIDs),
                  new System.Data.SqlClient.SqlParameter("@CreateUserID", room.CreateUserID),
                  new System.Data.SqlClient.SqlParameter("@CreateDateTime", DateTime.Now), };
                IMLibrary.SqlData.DataAccess.ExecSql(sql, sqlpar);
                return(true);
            }
            catch  { return(false); }
        }
Esempio n. 3
0
 /// <summary>
 /// 从用户中获取加入的群集合数组
 /// </summary>
 /// <param name="user"></param>
 /// <returns></returns>
 private Room[] GetRooms(UserVcard user)
 {
     lock (user.Rooms)//确保线程安全
     {
         Room[] Rooms = new Room[user.Rooms.Count];
         user.Rooms.Values.CopyTo(Rooms, 0);
         return Rooms;
     }
 }
Esempio n. 4
0
 /// <summary>
 /// 从群中获取用户集合数组
 /// </summary>
 /// <param name="room"></param>
 /// <returns></returns>
 private UserVcard[] GetUsers(Room room)
 {
     lock (room.Users)//确保线程安全
     {
         UserVcard[] Users = new UserVcard[room.Users.Count];
         room.Users.Values.CopyTo(Users, 0);
         return Users;
     }
 }
Esempio n. 5
0
        void msgServer_RequestChangeRoom(object sender, MsgServerEventArgs e)
        {
            ChangeRoom changeRoom = e.obj as ChangeRoom;

            if (changeRoom.type == type.New && e.RequestUser.CreatedRoomsCount < e.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 =e.RequestUser.UserID;//创建者为请求者

                        e.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 ==e.RequestUser.UserID)//如果群创建者为请求用户
                {
                    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, Factory.CreateXMLMsg(changeRoom));//通知群内原用户,群信息已经修改

                    DBHelper.UpdateRoom(room);
                }
                #endregion
            }
        }
Esempio n. 6
0
        /// <summary>
        /// 发送消息到群
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="room"></param>
        /// <param name="XMLMsg"></param>
        public void SendMessageToRoom(string userID, Room room, string XMLMsg)
        {
            if (!room.Users.ContainsKey(userID)) return;//如果群中无此用户,则不发送消息

            foreach (User user in GetUsers(room))
            {
                if (user.ShowType == IMLibrary3.Enmu.ShowType.Offline)
                    DBHelper.addMessageToDB(user.UserID, userID, room.RoomID, IMLibrary3.Enmu.MessageType.Group, XMLMsg);
                else
                    SendMessageToUser(user, XMLMsg);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// 获得群信息
        /// </summary>
        /// <param name="room"></param>
        private void onOrgRooms(Room room)
        {
            exRoom exroom = new exRoom();
            exroom.RoomID = room.RoomID;
            exroom.RoomName = room.RoomName;
            exroom.Notice = room.Notice;
            exroom.UserIDs = room.UserIDs;
            exroom.CreateUserID = room.CreateUserID;
            Rooms.Add(exroom);

            if (frmOrg != null && !frmOrg.IsDisposed)
            {
                frmOrg.Times = 0;
                frmOrg.Value = this.Rooms.Count;
            }
        }
Esempio n. 8
0
 /// <summary>
 /// 将所有数据库中群组读入内存
 /// </summary>
 /// <returns></returns>
 public  static Dictionary<string, Room> GetRooms()
 {
     Dictionary<string, Room>Rooms = new Dictionary<string, Room>(); 
     System.Data.SqlClient.SqlDataReader dr = DataAccess.GetReaderBySql("select * from Rooms");
     if (dr != null)
     {
         while (dr.Read())
         {
             Room room = new Room();
             room.RoomID = dr["RoomID"].ToString().Trim();
             room.RoomName = dr["RoomName"] is DBNull ? room.RoomID : dr["RoomName"].ToString().Trim();
             room.CreateUserID = dr["CreateUserID"] is DBNull ? "" : dr["CreateUserID"].ToString().Trim();
             room.UserIDs = dr["Users"] is DBNull ? "" : dr["Users"].ToString().Trim();
             room.Notice = dr["Notice"] is DBNull ? "" : dr["Notice"].ToString().Trim();
            
             Rooms.Add(room.RoomID, room);
         }
         dr.Close();
     }
     dr.Dispose();
     return Rooms;
 }