hub/client #3

Merged
s421801 merged 1 commits from clientmg into develop 2019-11-06 11:52:00 +01:00
4 changed files with 27 additions and 14 deletions
Showing only changes of commit f444c9fbae - Show all commits

View File

@ -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>();
});
}

View File

@ -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()

View File

@ -6,5 +6,7 @@ namespace Squirrowse.Client.Service
public interface IConnectionManager
{
Task<HubConnection> GetConnection();
Task InitConnection();
Task Disconnect();
}
}

View File

@ -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();
}
}
}