2023-01-28 14:41:03 +00:00
|
|
|
|
using System;
|
|
|
|
|
using Microsoft.AspNetCore.SignalR.Client;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Selector.SignalR;
|
|
|
|
|
namespace Selector.MAUI.Services;
|
|
|
|
|
|
|
|
|
|
public class HubManager
|
|
|
|
|
{
|
|
|
|
|
private readonly NowHubClient nowClient;
|
|
|
|
|
private readonly NowHubCache nowCache;
|
|
|
|
|
private readonly PastHubClient pastClient;
|
|
|
|
|
private readonly ILogger<HubManager> logger;
|
|
|
|
|
|
|
|
|
|
public HubManager(NowHubClient nowClient, NowHubCache nowCache, PastHubClient pastClient, ILogger<HubManager> logger)
|
|
|
|
|
{
|
|
|
|
|
this.nowClient = nowClient;
|
|
|
|
|
this.nowCache = nowCache;
|
|
|
|
|
this.pastClient = pastClient;
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task EnsureConnected()
|
|
|
|
|
{
|
2023-01-29 21:26:39 +00:00
|
|
|
|
var nowTask = Task.CompletedTask;
|
|
|
|
|
var pastTask = Task.CompletedTask;
|
|
|
|
|
|
2023-01-28 14:41:03 +00:00
|
|
|
|
if (nowClient.State == HubConnectionState.Disconnected)
|
|
|
|
|
{
|
|
|
|
|
logger.LogInformation("Starting now hub connection");
|
|
|
|
|
|
2023-01-29 21:26:39 +00:00
|
|
|
|
nowTask = nowClient.StartAsync().ContinueWith(async x =>
|
|
|
|
|
{
|
|
|
|
|
if (x.IsCompletedSuccessfully)
|
|
|
|
|
{
|
|
|
|
|
nowCache.BindClient();
|
|
|
|
|
await nowClient.OnConnected();
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-01-28 14:41:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pastClient.State == HubConnectionState.Disconnected)
|
|
|
|
|
{
|
|
|
|
|
logger.LogInformation("Starting past hub connection");
|
|
|
|
|
|
2023-01-29 21:26:39 +00:00
|
|
|
|
pastTask = pastClient.StartAsync().ContinueWith(async x =>
|
|
|
|
|
{
|
|
|
|
|
if (x.IsCompletedSuccessfully)
|
|
|
|
|
{
|
|
|
|
|
await pastClient.OnConnected();
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-01-28 14:41:03 +00:00
|
|
|
|
}
|
2023-01-29 21:26:39 +00:00
|
|
|
|
|
|
|
|
|
await Task.WhenAll(nowTask, pastTask);
|
2023-01-28 14:41:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|