public async Task HandleAsync(StudyRoomOnlineChangeEvent eventMessage, CancellationToken token)
        {
            var studyUser = eventMessage.StudyUser;
            var studyRoom = studyUser.Room;

            var onlineCount = studyRoom.Users.Count(f => f.Online);
            var totalOnline = 2; // //studyRoom.Users.Count;

            _telemetryClient.TrackEvent($"Users in room {studyRoom.Id}", metrics: new Dictionary <string, double>()
            {
                ["onlineCount"] = onlineCount,
                ["totalOnline"] = totalOnline
            });
            if (onlineCount == totalOnline)
            {
                var session = studyRoom.GetCurrentSession();
                if (session != null)
                {
                    var roomExists = await _videoProvider.GetRoomAvailableAsync(session.SessionId);

                    if (roomExists)
                    {
                        foreach (var user in studyRoom.Users)
                        {
                            var jwtToken =
                                await _videoProvider.ConnectToRoomAsync(session.SessionId, user.User.Id.ToString());


                            var message2 = new SignalRTransportType(SignalRType.StudyRoom,
                                                                    SignalRAction.Update, new
                            {
                                jwtToken
                            });

                            await _hubContext.Clients.User(user.User.Id.ToString())
                            .SendAsync(SbHub.MethodName, message2, token);
                        }

                        return;
                    }
                }
            }
            var message = new SignalRTransportType(SignalRType.StudyRoom,
                                                   SignalRAction.Update, new
            {
                onlineCount,
                totalOnline,
                users    = studyRoom.Users.Where(f => f.Online).Select(s => s.User.Id).ToArray(),
                allUsers = studyRoom.Users.Select(s => s.User.Id).ToArray()
            });


            await _hubContext.Clients.Group(studyRoom.Id.ToString()).SendAsync(SbHub.MethodName, message, token);
        }
Ejemplo n.º 2
0
        private async Task DoProcessAsync(StudyRoomSession studyRoomSession, CancellationToken token)
        {
            var users   = studyRoomSession.StudyRoom.Users;
            var session = studyRoomSession.SessionId;
            var tasks   = new List <Task>();

            foreach (var user in users)
            {
                var jwtToken = await _videoProvider.ConnectToRoomAsync(session, user.User.Id.ToString());

                var message = new SignalRTransportType(SignalRType.StudyRoom,
                                                       SignalREventAction.StartSession, new
                {
                    jwtToken
                });

                var t = _hubContext.Clients.User(user.User.Id.ToString()).SendAsync(SbHub.MethodName, message, token);
                tasks.Add(t);
            }

            await Task.WhenAll(tasks);
        }