Selector/Selector.SignalR/BaseSignalRClient.cs

52 lines
1.3 KiB
C#
Raw Normal View History

2023-01-22 10:28:52 +00:00
using System;
using Microsoft.AspNetCore.SignalR.Client;
namespace Selector.SignalR;
public abstract class BaseSignalRClient: IAsyncDisposable
{
private readonly string _baseUrl;
protected HubConnection hubConnection;
public string Token { get; set; }
2023-01-22 10:28:52 +00:00
public BaseSignalRClient(string path, string token)
2023-01-22 10:28:52 +00:00
{
//var baseOverride = Environment.GetEnvironmentVariable("SELECTOR_BASE_URL");
2023-01-22 10:28:52 +00:00
//if (!string.IsNullOrWhiteSpace(baseOverride))
//{
// _baseUrl = baseOverride;
//}
//else
//{
// _baseUrl = "https://selector.sarsoo.xyz";
//}
_baseUrl = "http://localhost:5000";
2023-01-22 10:28:52 +00:00
hubConnection = new HubConnectionBuilder()
.WithUrl(_baseUrl + "/" + path, options =>
{
options.AccessTokenProvider = () =>
{
return Task.FromResult(Token);
};
})
2023-01-22 10:28:52 +00:00
.WithAutomaticReconnect()
.Build();
}
public HubConnectionState State => hubConnection.State;
2023-01-22 10:28:52 +00:00
public ValueTask DisposeAsync()
{
return ((IAsyncDisposable)hubConnection).DisposeAsync();
}
public async Task StartAsync()
{
await hubConnection.StartAsync();
}
}