Beispiel #1
0
 private void CreateDanmuMonitorImpl()
 {
     _value = new DanmuMonitorImpl(_roomId, _id, _showHeartBeat, _manualCts.Token);
     _value.DanmuHandler += (object sender, DanmuHandlerEventArgs e) => DanmuHandler?.Invoke(sender, e);
     _value.Disposing    += (object sender, EventArgs e) => {
         GlobalSettings.Logger.LogError($"{_id} 号弹幕监控与服务器的连接意外断开,3s后尝试重连");
         Restart(new TimeSpan(0, 0, 3));
     };
 }
Beispiel #2
0
            private async Task ExecuteLoopImplAsync()
            {
                while (true)
                {
                    Danmu danmu;

                    if (_isDisposed)
                    {
                        return;
                    }
                    try {
                        using (CancellationTokenSource timeoutCts = new CancellationTokenSource(_receiveTimeout))
                            using (CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, _manualCts.Token, _cancellationToken))
                                danmu = await DanmuApi.ReadDanmuAsync(_client).WithCancellation(linkedCts.Token);
                    }
                    catch (OperationCanceledException) {
                        return;
                    }
                    catch (Exception ex) {
                        if (!_isDisposed)
                        {
                            // 可能是资源释放不同步导致错误,不记录
                            GlobalSettings.Logger.LogException(ex);
                        }
                        return;
                    }
                    if (_isDisposed)
                    {
                        return;
                    }
                    switch (danmu.Type)
                    {
                    case DanmuType.Command:
                        try {
                            DanmuHandler?.Invoke(this, new DanmuHandlerEventArgs(danmu));
                        }
                        catch (Exception ex) {
                            if (!_isDisposed)
                            {
                                GlobalSettings.Logger.LogException(ex);
                            }
                        }
                        break;

                    case DanmuType.Handshaking:
                        GlobalSettings.Logger.LogInfo($"{_id} 号弹幕监控进入房间 {_roomId}");
                        break;
                    }
                }
            }