using System; using System.Threading.Tasks; using LANCommander.SDK.Abstractions; using LANCommander.SDK.Extensions; using LANCommander.SDK.Rpc.Client; using LANCommander.SDK.Rpc.Server; using LANCommander.SDK.Services; using Microsoft.AspNetCore.SignalR.Client; namespace LANCommander.SDK.Rpc.Clients; internal partial class RpcSubscriber(ITokenProvider tokenProvider) : IRpcSubscriber { private HubConnection _connection = default!; public async Task ConnectAsync(Uri serverAddress) { try { _connection = new HubConnectionBuilder() .WithUrl(serverAddress.Join("rpc"), options => { options.AccessTokenProvider = () => Task.FromResult(tokenProvider.GetToken().AccessToken); }) .Build(); RpcClient.Hub = _connection.ServerProxy(); _ = _connection.ClientRegistration(this); await _connection.StartAsync(); return true; } catch { return false; } } public async Task DisconnectAsync() { try { await _connection.StopAsync(); return true; } catch { return false; } } }