Пример #1
0
        public async Task <IEnumerable <RoomDetails> > GetAllRoomsAsync()
        {
            var rooms = await RoomResource.ReadAsync();

            var tasks = rooms.Select(
                room => GetRoomDetailsAsync(
                    room,
                    ParticipantResource.ReadAsync(
                        room.Sid,
                        ParticipantStatus.Connected)));

            return(await Task.WhenAll(tasks));

            async Task <RoomDetails> GetRoomDetailsAsync(
                RoomResource room,
                Task <ResourceSet <ParticipantResource> > participantTask)
            {
                var participants = await participantTask;

                return(new RoomDetails
                {
                    Name = room.UniqueName,
                    MaxParticipants = room.MaxParticipants ?? 0,
                    ParticipantCount = participants.ToList().Count
                });
            }
        }
Пример #2
0
        public static async Task CreateMeetingRoomAsync(Event @event)
        {
            var uniqueName = GetRoomName(@event);

            var room = (await RoomResource.ReadAsync(
                            uniqueName: uniqueName,
                            limit: 1)).FirstOrDefault();

            if (room is null || room.UniqueName != uniqueName)
            {
                var callbackUrl = string.IsNullOrEmpty(_twilioConfiguration.RoomUpdateCallbackUrl) ?
                                  null :
                                  new Uri(_twilioConfiguration.RoomUpdateCallbackUrl);

                var callbackMethod = string.IsNullOrEmpty(_twilioConfiguration.RoomUpdateCallbackUrl) ?
                                     null :
                                     HttpMethod.Post;

                await RoomResource.CreateAsync(
                    uniqueName : uniqueName,
                    type : RoomResource.RoomTypeEnum.Group,
                    maxParticipants : _maxParticipants,
                    recordParticipantsOnConnect : false,
                    statusCallback : callbackUrl,
                    statusCallbackMethod : callbackMethod);
            }
        }
Пример #3
0
        public async ValueTask <IEnumerable <RoomDetails> > GetAllRoomsAsync()
        {
            var rooms = await RoomResource.ReadAsync();

            var tasks = rooms.Select(
                room => GetRoomDetailsAsync(
                    room,
                    ParticipantResource.ReadAsync(
                        room.Sid,
                        ParticipantStatus.Connected)));

            return(await Task.WhenAll(tasks));
Пример #4
0
        public async Task <bool> GetRoomAvailableAsync(string id)
        {
            var rooms = await RoomResource.ReadAsync(
                uniqueName : id);

            var room = rooms.SingleOrDefault();

            if (room == null)
            {
                return(false);
            }
            if (room.Status == RoomResource.RoomStatusEnum.Completed)
            {
                return(false);
            }

            return(true);
        }
Пример #5
0
        public async Task <IEnumerable <RoomDetails> > GetAllRoomsAsync(string roomSid = null)
        {
            var rooms = await RoomResource.ReadAsync();

            if (roomSid != null)
            {
                var tasks = rooms.Where(x => x.Sid == roomSid).Select(
                    room => GetRoomDetailsAsync(
                        room,
                        ParticipantResource.ReadAsync(
                            room.Sid,
                            ParticipantStatus.Connected)));
                return(await Task.WhenAll(tasks));
            }
            else
            {
                var tasks = rooms.Select(
                    room => GetRoomDetailsAsync(
                        room,
                        ParticipantResource.ReadAsync(
                            room.Sid,
                            ParticipantStatus.Connected)));

                return(await Task.WhenAll(tasks));
            }
            //Note that for every room n that exists, GetRoomDetailsAsync is invoked to fetch the room’s connected participants. This can be a performance concern! Even though this is done asynchronously and in parallel, it should be considered a potential bottleneck and marked for refactoring. It isn't a concern in this demo project, as there are, at most, a few rooms.
            async Task <RoomDetails> GetRoomDetailsAsync(
                RoomResource room,
                Task <ResourceSet <ParticipantResource> > participantTask)
            {
                var participants = await participantTask;

                return(new RoomDetails
                {
                    Id = room.Sid,
                    Name = room.UniqueName,
                    MaxParticipants = room.MaxParticipants ?? 0,
                    ParticipantCount = participants.ToList().Count
                });
            }
        }
Пример #6
0
        public async Task <string> ConnectToRoomAsync(string roomName, string name)
        {
            try
            {
                var rooms = await RoomResource.ReadAsync(
                    status : RoomResource.RoomStatusEnum.InProgress,
                    uniqueName : roomName);

                var room = rooms.FirstOrDefault();
                if (room == null)
                {
                    return(null);
                }
                var grant = new VideoGrant
                {
                    Room = room.UniqueName,
                };
                var grants = new HashSet <IGrant> {
                    grant
                };

                // Create an Access Token generator
                var token = new Token(
                    AccountSid,
                    ApiKey,
                    SecretVideo,
                    identity: name,
                    grants: grants);

                return(token.ToJwt());
            }
            catch (Twilio.Exceptions.ApiException)
            {
                return(null);
            }
        }