コード例 #1
0
        public async Task AppendSession(string id, AppendSessionRequest item, string connectionId, string nickName)
        {
            if (!this.Sessions.ContainsKey(id))
            {
                var session = new SessionManager()
                {
                    Id              = id,
                    ContentType     = item.ContentType,
                    FileName        = item.FileName,
                    BroadcasterId   = connectionId,
                    BroadcasterName = nickName
                };

                this.Sessions.Add(id, session);

                var manager      = ListenerManager.GetInstance();
                var notification = new AppendSessionNotification()
                {
                    Id              = id,
                    ContentType     = item.ContentType,
                    FileName        = item.FileName,
                    BroadcasterName = nickName
                };
                await Task.Run(() =>
                {
                    IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext <ListenHub>();
                    hubContext.Clients.Group(this.RoomId).AppendSession(notification);
                });
            }
        }
コード例 #2
0
        public static ListenerManager GetInstance()
        {
            if (_instance == null)
                _instance = new ListenerManager();

            return _instance;
        }
コード例 #3
0
        public async Task RemoveBroadcaster(string connectionId, string roomId)
        {
            if (this._rooms.ContainsKey(roomId))
            {
                var roomInfo = this._rooms[roomId];
                // Sessionが削除されたことを通知
                var sessions = roomInfo.Sessions.Where(c => c.Value.BroadcasterId == connectionId).Select(c => c.Key).ToList();

                foreach (var session in sessions)
                {
                    await roomInfo.RemoveSession(new RemoveSessionRequest()
                    {
                        SessionId = session
                    });
                }

                roomInfo.Broadcasters.Remove(connectionId);
                if (roomInfo.Broadcasters.Count == 0)
                {
                    using (var db = new ApplicationDbContext())
                    {
                        var room = await db.Rooms
                                   .FirstOrDefaultAsync(c => c.Id == roomId);

                        if (room != null)
                        {
                            // EFのLazyLoadingのため...
                            var owner = room.Owner;

                            // 自動的にDisconnectする
                            room.TotalVisitor        = roomInfo.VisitorCount;
                            room.LatestBroadcastDate = DateTime.Now;
                            room.IsLive = false;
                            await db.SaveChangesAsync();
                        }
                    }


                    // Listenerへ通知
                    await roomInfo.NotifyStopBroadcast();

                    // Listenerの削除
                    var listenerManager = ListenerManager.GetInstance();
                    lock (roomInfo.Listeners)
                    {
                        IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext <ListenHub>();

                        foreach (var listener in roomInfo.Listeners)
                        {
                            listenerManager.RemoveListenerWithoutRoomOperation(listener);
                            hubContext.Groups.Remove(listener, roomId).Wait();
                        }
                    }

                    // 放送の終了
                    //await room.NotifyStopBroadcast();
                    this._rooms.Remove(roomId);
                }
            }
        }
コード例 #4
0
        public static ListenerManager GetInstance()
        {
            if (_instance == null)
            {
                _instance = new ListenerManager();
            }

            return(_instance);
        }
コード例 #5
0
        //public async Task NotifyStartBroadcast()
        //{
        //    var item = new BroadcastEventNotification()
        //    {
        //        EventType = BroadcastEventType.StartBroadcast
        //    };

        //    var manager = ListenerManager.GetInstance();

        //    await Task.Run(() =>
        //    {
        //        IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<ListenHub>();
        //        hubContext.Clients.Group(this.RoomId).NotifyBroadcastEvent(item);
        //    });
        //}

        public async Task NotifyStopBroadcast()
        {
            var item = new BroadcastEventNotification()
            {
                EventType = BroadcastEventType.StopBroadcast
            };
            var manager = ListenerManager.GetInstance();

            await Task.Run(() =>
            {
                IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext <ListenHub>();
                hubContext.Clients.Group(this.RoomId).NotifyBroadcastEvent(item);
            });
        }
コード例 #6
0
        public async Task UpdateRoomStatus()
        {
            var item = new UpdateBroadcastStatusNotification()
            {
                VisitorCount = this.VisitorCount,
                ViewCount    = this.Listeners.Count
            };
            var manager = ListenerManager.GetInstance();

            await Task.Run(() =>
            {
                IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext <ListenHub>();
                hubContext.Clients.Group(this.RoomId).UpdateRoomStatus(item);
            });
        }
コード例 #7
0
        public async Task UpdateSessionCursor(UpdateCursorRequest item)
        {
            var session = this.GetSession(item.SessionId);

            if (session != null)
            {
                if (session.UpdateCursor(item))
                {
                    var manager = ListenerManager.GetInstance();
                    await Task.Run(() =>
                    {
                        IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext <ListenHub>();
                        hubContext.Clients.Group(this.RoomId).UpdateSessionCursor(item);
                    });
                }
            }
        }
コード例 #8
0
        //public async Task SwitchActiveSession(SwitchActiveSessionNotification item)
        //{
        //    if (this.Sessions.ContainsKey(item.SessionId))
        //    {
        //        this.CurrentSession = this.Sessions[item.SessionId];

        //        var manager = ListenerManager.GetInstance();
        //        await Task.Run(() =>
        //        {
        //            Parallel.ForEach(this.Listeners, c =>
        //            {
        //                var listener = manager.GetConnectionInfo(c);
        //                if (listener != null)
        //                {
        //                    lock (listener)
        //                    {
        //                        IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<ListenHub>();
        //                        hubContext.Clients.Group(this.RoomId).SwitchActiveSession(item);
        //                    }
        //                }
        //            });
        //        });
        //    }
        //}
        #endregion

        public async Task UpdateSessionInfo(UpdateSessionInfoRequest item)
        {
            if (this.Sessions.ContainsKey(item.SessionId))
            {
                var session = this.Sessions[item.SessionId];
                session.ContentType = item.ContentType;
                session.FileName    = item.FileName;

                var notification = new AppendSessionNotification()
                {
                    Id              = item.SessionId,
                    ContentType     = item.ContentType,
                    FileName        = item.FileName,
                    BroadcasterName = session.BroadcasterName
                };

                var manager = ListenerManager.GetInstance();
                await Task.Run(() =>
                {
                    IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext <ListenHub>();
                    hubContext.Clients.Group(this.RoomId).UpdateSessionInfo(notification);
                });
            }
        }
コード例 #9
0
        public async Task RemoveSession(RemoveSessionRequest item)
        {
            if (this.Sessions.ContainsKey(item.SessionId))
            {
                this.Sessions.Remove(item.SessionId);

                var manager = ListenerManager.GetInstance();
                await Task.Run(() =>
                {
                    Parallel.ForEach(this.Listeners, c =>
                    {
                        var listener = manager.GetConnectionInfo(c);
                        if (listener != null)
                        {
                            lock (listener)
                            {
                                IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext <ListenHub>();
                                hubContext.Clients.Group(this.RoomId).RemoveSession(item);
                            }
                        }
                    });
                });
            }
        }