Example #1
0
        public StreamMonitor(RoomSetting setting)
        {
            RoomId = setting.RoomId;
#if DEBUG
            StreamStarted += (o, args) =>
            {
                LogEvent?.Invoke(this,
                                 args.IsLive
                                                                ? new LogEventArgs {
                    Log = $@"[{RoomId}] [{args.Type}] [{setting.UserName}] 开播:{setting.Title}"
                }
                                                                : new LogEventArgs {
                    Log = $@"[{RoomId}] [{args.Type}] [{setting.UserName}] 下播/未开播"
                });
            };
#endif
            _danMuClient                  = new DanMuClient(RoomId, TimeSpan.FromMilliseconds(setting.TimingDanmakuRetry));
            _danMuClient.LogEvent        += (o, args) => LogEvent?.Invoke(o, args);
            _danMuClient.ReceivedDanmaku += (o, args) =>
            {
                switch (args.Danmaku.MsgType)
                {
                case MsgType.LiveStart:
                    StreamStarted?.Invoke(this, new StreamStartedArgs
                    {
                        Type   = TriggerType.弹幕,
                        IsLive = true
                    });
                    break;

                case MsgType.LiveEnd:
                    StreamStarted?.Invoke(this, new StreamStartedArgs
                    {
                        Type   = TriggerType.弹幕,
                        IsLive = false
                    });
                    break;
                }
            };
            _httpTimer = new Timer(TimeSpan.FromSeconds(setting.TimingCheckInterval).TotalMilliseconds)
            {
                Enabled             = false,
                AutoReset           = true,
                SynchronizingObject = null,
                Site = null
            };
            _httpTimer.Elapsed += (sender, e) =>
            {
                Check(TriggerType.HttpApi);
            };
        }
        //[TestMethod]
        public async Task DanMuTest()
        {
            const int roomId = 40462;

            using var client        = new DanMuClient(roomId, TimeSpan.FromSeconds(2));
            client.ReceivedDanmaku += (o, args) =>
            {
                var danMu = args.Danmaku;
                Debug.WriteLine(danMu.UserName);
                Debug.WriteLine(danMu.CommentText);
                Debug.WriteLine(danMu.MsgType);
            };
            client.Start();
            await Task.Delay(TimeSpan.FromSeconds(30));
        }