Skip to content

Instantly share code, notes, and snippets.

@maor365scores
Last active June 17, 2024 10:31
Show Gist options
  • Save maor365scores/3d924a3c176d8a45f53470bfea2224df to your computer and use it in GitHub Desktop.
Save maor365scores/3d924a3c176d8a45f53470bfea2224df to your computer and use it in GitHub Desktop.
public class SubscriptionManager
{
private int _subscribeGamesCount;
private ScanLoop _subscriptionLoop;
private ScanLoop _unSubscriptionLoop;
private readonly SDDPParser _sddpParser;
private readonly SDDPMapper _sddpMapper;
private readonly ScannerConfigurations _scannerConfigurations;
private readonly ScannerMapperConfigurations _scannerMapperConfigurations;
private readonly WebSocketConfigurations _webSocketConfigurations;
private readonly ServiceConfigurations _serviceConfigurations;
private Action<ICollection<CSoccerGameUpdate>> _raiseEventAction;
private readonly ILogger<SubscriptionManager> _logger;
private GamesRepo _gamesRepo;
private IScannerWebsocketClient _client;
private const string WELCOME_MSG = "welcome";
private const string MATCH_DETAILS_MSG = "matchdetails";
private string[] FEED_NAME = { "matchEventStats", "stats", "matchEvent" }; //livescores
private object[] SNAPSHOTS = { new { feed = "stats" } };
private const string SUBSCRIBE = "subscribe";
private const string UNSUBSCRIBE = "unsubscribe";
private const int NumberOfMinsToInvokeUSubscriptionHandlerDefault = 5;
private const int NumberOfHoursToUnSubscribeDefault = 4;
private const string IS_SUBSCRIBE = "is_subscribed";
private const string IS_AUTORISED = "is_authorised";
private readonly ILogger<ScannerWebSocketClient> _webSocketlogger;
private readonly HostContext _hostContext;
private readonly IMonitorService _monitor;
public async Task Subscribe()
{
_gamesRepo.IsInitialized_ResetEvent.Wait();
var gamesForSubscribe = GetGamesForSubscribe(_gamesRepo.Games);
if (gamesForSubscribe != null)
{
foreach (var game in gamesForSubscribe)
{
try
{
var subscriptionObject = new
{
content = new SubscriptionObject()
{
name = SUBSCRIBE, feed = FEED_NAME, fixtureUuid = game.Fixture.FixtureId, optaId = true,
snapShot = SNAPSHOTS
}
};
_client.Send(message: JObject.FromObject(subscriptionObject).ToString());
game.SubscriptionTime = DateTime.UtcNow;
_logger.LogTrace($"[{SoccerOptaSportsDataDeltaPushScanner.NAME}] {nameof(SubscriptionManager)}: subscribe to game {game.Fixture.FixtureId}: {game.Fixture.HomeTeamName}-{game.Fixture.AwayTeamName}");
}
catch (Exception e)
{
_logger.LogError(e,
$"[{SoccerOptaSportsDataDeltaPushScanner.NAME}] {nameof(SubscriptionManager)}: failed to subscribe to game {game.Fixture.FixtureId}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment