Ejemplo n.º 1
0
        /* Add User to specified room.
         * If room is not specified, add to 'public' room.
         * If user in other room, switch to specified room.
         * */
        public bool AddUserToRoom(IUser user, string roomName = publicRoomName)
        {
            if (!ContainRoom(roomName))
                return false;  //room not exist

            if (!ContainUser(user))
                return false;  //user not exist

            if(userMap[user] == roomName)
                return true;  //stay in room.

            if (userMap[user] != nullRoomName)  //if user already in one room, leave firstly.
                RemoveUserFromRoom(user);

            userMap[user] = roomName;
            roomMap[roomName].Add(user);
            //Notify others.
            UserSendMassageToRoom(user, "\r\n<= *new user joined chat: "+user.GetStatus().UserName+"\r\n=> ");
            return true;
        }
Ejemplo n.º 2
0
        /* Remove user from room. when last user leave, room will be removed too
         * 'public' room always exist.
         * */
        public void RemoveUserFromRoom(IUser user)
        {
            if (userMap[user] == nullRoomName)  //user not in any room.
                return;

            string roomname = userMap[user];
            roomMap[roomname].Remove(user);
            userMap[user] = nullRoomName;

            RoomBroadcast(roomname, "\r\n<= *user has left chat: "+user.GetStatus().UserName+"\r\n=> ");
            user.SendToClient("\r\n<= *user has left chat: " + user.GetStatus().UserName + " (**this is you)\r\n");

            //when last user leave
            if (roomMap[roomname].Count == 0 && roomname != publicRoomName)
            {
                RemoveRoom(roomname);
            }
        }