Seperated eventhub service from api service.

wip nbs flow.
Changed uploading a bit.
This commit is contained in:
2020-03-25 21:32:28 +01:00
parent e638370ad4
commit 691be2185e
11 changed files with 151 additions and 81 deletions

View File

@@ -0,0 +1,45 @@
using System;
using System.Threading.Tasks;
using FarmmapsApi.Models;
using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging;
namespace FarmmapsApi.Services
{
public class FarmmapsEventHub
{
private readonly Configuration _configuration;
private readonly HttpClientSettings _httpClientSettings;
private HubConnection _hubConnection;
public event Action<EventMessage> EventCallback;
public FarmmapsEventHub(HttpClientSettings httpClientSettings, Configuration configuration)
{
_httpClientSettings = httpClientSettings;
_configuration = configuration;
}
public async Task StartEventHub()
{
if(_hubConnection != null)
throw new Exception("EventHub already started");
var eventEndpoint = $"{_configuration.Endpoint}/EventHub";
_hubConnection = new HubConnectionBuilder()
.WithUrl(eventEndpoint, HttpTransportType.WebSockets,
options => options.SkipNegotiation = true)
.ConfigureLogging(log => log.AddConsole())
.WithAutomaticReconnect()
.Build();
await _hubConnection.StartAsync();
await _hubConnection.SendAsync("authenticate", _httpClientSettings.BearerToken);
_hubConnection.Reconnected += async s => await _hubConnection.SendAsync("authenticate", _httpClientSettings.BearerToken);
_hubConnection.On("Event", (EventMessage eventMessage) => EventCallback?.Invoke(eventMessage));
}
}
}