LANCommander/LANCommander.Launcher/UI/Authenticate/Components/ServerSelector.razor
Pat Hartl 041a0069ee Add DiscoveryBeacon, DiscoveryProbe, new beacon message
Adds a discovery beacon and probe to the client and removes BeaconService from the server.
2025-04-23 21:23:33 -05:00

100 lines
No EOL
2.9 KiB
Text

@using LANCommander.Launcher.Models
@using LANCommander.SDK.Models
@using ListItem = AntDesign.ListItem
@inject NavigationManager NavigationManager
@inject SDK.Client Client
@inject AuthenticationService AuthenticationService
<PageHeader>
<TitleTemplate>
Connect
</TitleTemplate>
</PageHeader>
<Form Model="Model" OnFinish="() => SelectServer(ServerAddress)">
<FormItem>
<Flex Gap="FlexGap.Small">
<Input @bind-Value="ServerAddress" Placeholder="Server Address" AutoFocus Disabled="Connecting" />
<Button Type="ButtonType.Primary" HtmlType="submit" Disabled="Connecting" Loading="Connecting">Connect</Button>
</Flex>
</FormItem>
</Form>
<Divider Text="Discovered" Orientation="DividerOrientation.Center" />
<AntList DataSource="DiscoveredServers">
<ChildContent>
<ListItem OnClick="() => SelectServer(context.Address.ToString())">
<ListItemMeta Title="@context.Name" Description="@context.Address.ToString()"/>
</ListItem>
</ChildContent>
<LoadMore>
<Flex Justify="FlexJustify.Center" Class="load-more">
@if (BeaconActive)
{
<Button Type="ButtonType.Primary" Loading="true" Disabled>Scanning</Button>
}
else
{
<Button Type="ButtonType.Primary" OnClick="() => ActivateBeacon()">Rescan</Button>
}
</Flex>
</LoadMore>
</AntList>
@code {
[Parameter] public EventCallback<string> OnSelected { get; set; }
[Parameter] public bool Connecting { get; set; } = false;
bool BeaconActive = false;
bool OfflineModeAvailable = false;
string ServerAddress = String.Empty;
AuthRequest Model = new();
List<DiscoveredServer> DiscoveredServers = new();
Models.Settings Settings = SettingService.GetSettings();
protected override async Task OnInitializedAsync()
{
OfflineModeAvailable = await AuthenticationService.OfflineModeAvailableAsync();
Client.Beacon.OnBeaconResponse += OnBeaconResponse;
ActivateBeacon();
}
private void OnBeaconResponse(object sender, BeaconResponseArgs e)
{
var discoveredServer = new DiscoveredServer(e.Message, e.EndPoint);
if (DiscoveredServers.All(s => s.Address != discoveredServer.Address))
DiscoveredServers.Add(discoveredServer);
}
async Task SelectServer(string serverAddress)
{
await Client.Beacon.StopProbeAsync();
BeaconActive = false;
if (OnSelected.HasDelegate)
await OnSelected.InvokeAsync(serverAddress);
}
async Task ActivateBeacon()
{
BeaconActive = true;
await Client.Beacon.StartProbeAsync();
await Task.Delay(10000);
await Client.Beacon.StopProbeAsync();
BeaconActive = false;
await InvokeAsync(StateHasChanged);
}
}