Exemplo n.º 1
0
 public void Check(TriggerType type = TriggerType.HttpApiRecheck)
 {
     if (BililiveAPI.GetRoomInfo(Roomid).isStreaming)
     {
         _StartRecord(type);
     }
 }
Exemplo n.º 2
0
        public async Task <RoomInfo> FetchRoomInfoAsync()
        {
            RoomInfo roomInfo = await BililiveAPI.GetRoomInfoAsync(Roomid);

            RoomInfoUpdated?.Invoke(this, new RoomInfoUpdatedArgs {
                RoomInfo = roomInfo
            });
            return(roomInfo);
        }
Exemplo n.º 3
0
        public StreamMonitor(int roomid, Func <TcpClient> funcTcpClient, ConfigV1 config, BililiveAPI bililiveAPI)
        {
            this.funcTcpClient = funcTcpClient;
            this.config        = config;
            this.bililiveAPI   = bililiveAPI;

            Roomid = roomid;

            ReceivedDanmaku += Receiver_ReceivedDanmaku;
            RoomInfoUpdated += StreamMonitor_RoomInfoUpdated;

            dmTokenSource = new CancellationTokenSource();
            Repeat.Interval(TimeSpan.FromSeconds(30), () =>
            {
                if (dmNetStream != null && dmNetStream.CanWrite)
                {
                    try
                    {
                        SendSocketData(2);
                    }
                    catch (Exception) { }
                }
            }, dmTokenSource.Token);

            httpTimer = new Timer(config.TimingCheckInterval * 1000)
            {
                Enabled             = false,
                AutoReset           = true,
                SynchronizingObject = null,
                Site = null
            };
            httpTimer.Elapsed += (sender, e) =>
            {
                try
                {
                    Check(TriggerType.HttpApi);
                }
                catch (Exception ex)
                {
                    logger.Log(Roomid, LogLevel.Warn, "获取直播间开播状态出错", ex);
                }
            };

            config.PropertyChanged += (sender, e) =>
            {
                if (e.PropertyName.Equals(nameof(config.TimingCheckInterval)))
                {
                    httpTimer.Interval = config.TimingCheckInterval * 1000;
                }
            };
        }
Exemplo n.º 4
0
 private void HttpCheck()
 {
     try
     {
         if (BililiveAPI.GetRoomInfo(Roomid).isStreaming)
         {
             _StartRecord(TriggerType.HttpApi);
         }
     }
     catch (Exception ex)
     {
         logger.Log(Roomid, LogLevel.Warn, "获取直播间开播状态出错", ex);
     }
 }
Exemplo n.º 5
0
        public Recorder(ConfigV1 config, Func <int, IRecordedRoom> iRecordedRoom)
        {
            Config           = config;
            newIRecordedRoom = iRecordedRoom;

            tokenSource = new CancellationTokenSource();
            Repeat.Interval(TimeSpan.FromSeconds(3), DownloadWatchdog, tokenSource.Token);

            Rooms.CollectionChanged += (sender, e) =>
            {
                logger.Debug($"Rooms.CollectionChanged;{e.Action};" +
                             $"O:{e.OldItems?.Cast<IRecordedRoom>()?.Select(rr => rr.RoomId.ToString())?.Aggregate((current, next) => current + "," + next)};" +
                             $"N:{e.NewItems?.Cast<IRecordedRoom>()?.Select(rr => rr.RoomId.ToString())?.Aggregate((current, next) => current + "," + next)}");
            };

            var debouncing = new SemaphoreSlim(1, 1);

            Config.PropertyChanged += async(sender, e) =>
            {
                if (e.PropertyName == nameof(Config.UseProxyForApi) ||
                    e.PropertyName == nameof(Config.ProxyAddress) ||
                    e.PropertyName == nameof(Config.ProxyRequireCredentials) ||
                    e.PropertyName == nameof(Config.ProxyUsername) ||
                    e.PropertyName == nameof(Config.ProxyPassword)
                    )
                {
                    if (await debouncing.WaitAsync(0))
                    {
                        try
                        {
                            logger.Debug("设置代理等待...");
                            await Task.Delay(50);

                            logger.Debug("设置代理信息...");
                            await BililiveAPI.ApplyProxySettings(
                                Config.UseProxyForApi, Config.ProxyAddress,
                                Config.ProxyRequireCredentials, Config.ProxyUsername, Config.ProxyPassword);

                            logger.Debug("设置成功");
                        }
                        finally
                        {
                            debouncing.Release();
                        }
                    }
                }
            };
        }
Exemplo n.º 6
0
        private async Task <bool> ConnectAsync()
        {
            if (dmTcpConnected)
            {
                return(true);
            }

            try
            {
                var(token, host, port) = await BililiveAPI.GetDanmuConf(Roomid);

                logger.Log(Roomid, LogLevel.Debug, $"连接弹幕服务器 {host}:{port} {(string.IsNullOrWhiteSpace(token) ? "无" : "有")} token");

                dmClient = funcTcpClient();
                dmClient.Connect(host, port);
                dmNetStream = dmClient.GetStream();

                dmReceiveMessageLoopThread = new Thread(ReceiveMessageLoop)
                {
                    Name         = "ReceiveMessageLoop " + Roomid,
                    IsBackground = true
                };
                dmReceiveMessageLoopThread.Start();

                var hello = JsonConvert.SerializeObject(new
                {
                    uid       = 0,
                    roomid    = Roomid,
                    protover  = 2,
                    platform  = "web",
                    clientver = "1.11.0",
                    type      = 2,
                    key       = token,
                }, Formatting.None);
                SendSocketData(7, hello);
                SendSocketData(2);

                return(true);
            }
            catch (Exception ex)
            {
                dmError = ex;
                logger.Log(Roomid, LogLevel.Error, "连接弹幕服务器错误", ex);

                return(false);
            }
        }
Exemplo n.º 7
0
        public void Check(TriggerType type, int millisecondsDelay = 0)
        {
            if (disposedValue)
            {
                throw new ObjectDisposedException(nameof(StreamMonitor));
            }

            if (millisecondsDelay < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(millisecondsDelay), "不能小于0");
            }

            Task.Run(() =>
            {
                Task.Delay(millisecondsDelay).Wait();
                if (BililiveAPI.GetRoomInfo(Roomid).isStreaming)
                {
                    StreamStatusChanged?.Invoke(this, new StreamStatusChangedArgs()
                    {
                        type = type
                    });
                }
            });
        }