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