feat(Auth.Client): Erstellt, um Client (Consumer) Ereignisse von SignalR zu behandeln.
- Erstellt ClientParams um AuthClient zu konfigurieren. - Erstellt ClientEvents, um Ereignisse im Client zu konfigurieren.
This commit is contained in:
parent
a2f4fcfbe0
commit
bf12c889f3
@ -3,6 +3,4 @@
|
||||
public interface IAuthClient
|
||||
{
|
||||
Task ReceiveMessage(string user, string message);
|
||||
|
||||
Task<string> GetMessage();
|
||||
}
|
||||
32
DigitalData.Auth.Client/AuthClient.cs
Normal file
32
DigitalData.Auth.Client/AuthClient.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using DigitalData.Auth.Abstractions;
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace DigitalData.Auth.Client;
|
||||
|
||||
public class AuthClient : IAuthClient
|
||||
{
|
||||
private readonly HubConnection _connection;
|
||||
|
||||
private readonly ILogger? _logger;
|
||||
|
||||
private readonly ClientParams _params;
|
||||
|
||||
public AuthClient(IOptions<ClientParams> paramsOptions, HubConnectionBuilder connectionBuilder, ILogger<AuthClient>? logger = null)
|
||||
{
|
||||
_connection = connectionBuilder
|
||||
.WithUrl(paramsOptions.Value.Url)
|
||||
.Build();
|
||||
|
||||
_connection.On<string, string>(nameof(ReceiveMessage), ReceiveMessage);
|
||||
|
||||
_logger = logger;
|
||||
|
||||
_params = paramsOptions.Value;
|
||||
}
|
||||
|
||||
public async Task StartAsync() => await _connection.StartAsync();
|
||||
|
||||
public Task ReceiveMessage(string user, string message) => _params.Events.OnMessageReceived(user, message, _logger);
|
||||
}
|
||||
12
DigitalData.Auth.Client/ClientEvents.cs
Normal file
12
DigitalData.Auth.Client/ClientEvents.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DigitalData.Auth.Client
|
||||
{
|
||||
public class ClientEvents
|
||||
{
|
||||
public Func<string, string, ILogger?, Task> OnMessageReceived { get; set; } = (user, message, logger)
|
||||
=> Task.Run(
|
||||
() => logger?.LogInformation("{user}: {message}", user, message)
|
||||
);
|
||||
}
|
||||
}
|
||||
8
DigitalData.Auth.Client/ClientParams.cs
Normal file
8
DigitalData.Auth.Client/ClientParams.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace DigitalData.Auth.Client;
|
||||
|
||||
public class ClientParams
|
||||
{
|
||||
public required string Url { get; init; }
|
||||
|
||||
public readonly ClientEvents Events = new();
|
||||
}
|
||||
@ -9,4 +9,8 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DigitalData.Auth.Abstractions\DigitalData.Auth.Abstractions.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user