Refactor hub/client
This commit is contained in:
parent
2e624d9b7a
commit
f444c9fbae
@ -19,7 +19,7 @@ namespace Squirrowse.Client
|
|||||||
services.AddHostedService<Worker>();
|
services.AddHostedService<Worker>();
|
||||||
services.AddSingleton<IConnectionManager>(x=> new ConnectionManager("http://localhost", 5000)); //keep as transient for now
|
services.AddSingleton<IConnectionManager>(x=> new ConnectionManager("http://localhost", 5000)); //keep as transient for now
|
||||||
services.AddSingleton< ICameraService, CameraService >(x=>new CameraService(new Camera()));
|
services.AddSingleton< ICameraService, CameraService >(x=>new CameraService(new Camera()));
|
||||||
services.AddTransient<IActionDispatcher, ActionDispatcher>();
|
services.AddSingleton<IActionDispatcher, ActionDispatcher>();
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -27,10 +27,25 @@ namespace Squirrowse.Client.Service
|
|||||||
return _connection;
|
return _connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new Exception();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task InitConnection()
|
||||||
|
{
|
||||||
|
if (_connection.State == HubConnectionState.Connected)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
await _connection.StartAsync();
|
await _connection.StartAsync();
|
||||||
await RegisterOnHub();
|
await RegisterOnHub();
|
||||||
Connected = true;
|
Connected = true;
|
||||||
return _connection;
|
}
|
||||||
|
|
||||||
|
public async Task Disconnect()
|
||||||
|
{
|
||||||
|
if (_connection.State == HubConnectionState.Disconnected) throw new Exception();
|
||||||
|
await _connection.StopAsync();
|
||||||
|
Connected = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task RegisterOnHub()
|
private async Task RegisterOnHub()
|
||||||
|
@ -6,5 +6,7 @@ namespace Squirrowse.Client.Service
|
|||||||
public interface IConnectionManager
|
public interface IConnectionManager
|
||||||
{
|
{
|
||||||
Task<HubConnection> GetConnection();
|
Task<HubConnection> GetConnection();
|
||||||
|
Task InitConnection();
|
||||||
|
Task Disconnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -9,7 +9,7 @@ using Squirrowse.Client.Service;
|
|||||||
|
|
||||||
namespace Squirrowse.Client
|
namespace Squirrowse.Client
|
||||||
{
|
{
|
||||||
public class Worker : BackgroundService
|
public class Worker : IHostedService
|
||||||
{
|
{
|
||||||
private readonly ILogger<Worker> logger;
|
private readonly ILogger<Worker> logger;
|
||||||
private readonly IConnectionManager _connectionManager;
|
private readonly IConnectionManager _connectionManager;
|
||||||
@ -22,18 +22,14 @@ namespace Squirrowse.Client
|
|||||||
this.camera = camera;
|
this.camera = camera;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
public async Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
|
await _connectionManager.InitConnection();
|
||||||
|
}
|
||||||
|
|
||||||
while (!stoppingToken.IsCancellationRequested)
|
public async Task StopAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
await _connectionManager.Disconnect();
|
||||||
|
|
||||||
await _connectionManager.GetConnection();
|
|
||||||
//await _connectionManager.SendStreamAsync(camera.GetFramesAsyncEnumerator());
|
|
||||||
|
|
||||||
// await Task.Delay(50, stoppingToken);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user