AyCode.Core/AyCode.Services/SignalRs/AcSignalRConnectionExtensio...

50 lines
2.2 KiB
C#

using Microsoft.AspNetCore.SignalR.Client;
namespace AyCode.Services.SignalRs;
/// <summary>
/// Extension methods for applying <see cref="AcHubConnectionOptions"/> to
/// a client <see cref="IHubConnectionBuilder"/>.
/// </summary>
public static class AcSignalRConnectionExtensions
{
/// <summary>
/// Applies <see cref="AcHubConnectionOptions"/> to the builder:
/// <c>WithUrl</c> (with HttpConnectionOptions), keep-alive, server timeout,
/// <c>WithAutomaticReconnect</c>, <c>WithStatefulReconnect</c>.
/// <para>
/// Nullable properties are skipped when <c>null</c> — the underlying SignalR default is kept.
/// Combine freely with any other builder extensions before or after this call; the ordering
/// follows the fluent chain (last write wins for a given setting).
/// </para>
/// </summary>
public static IHubConnectionBuilder AddAcConnection(
this IHubConnectionBuilder builder,
AcHubConnectionOptions options)
{
if (options is null) throw new ArgumentNullException(nameof(options));
if (string.IsNullOrWhiteSpace(options.Url))
throw new ArgumentException("AcHubConnectionOptions.Url must be set.", nameof(options));
builder.WithUrl(options.Url, http =>
{
if (options.Transports.HasValue) http.Transports = options.Transports.Value;
if (options.TransportMaxBufferSize.HasValue) http.TransportMaxBufferSize = options.TransportMaxBufferSize.Value;
if (options.ApplicationMaxBufferSize.HasValue) http.ApplicationMaxBufferSize = options.ApplicationMaxBufferSize.Value;
if (options.CloseTimeout.HasValue) http.CloseTimeout = options.CloseTimeout.Value;
if (options.SkipNegotiation.HasValue) http.SkipNegotiation = options.SkipNegotiation.Value;
});
if (options.KeepAliveInterval.HasValue)
builder.WithKeepAliveInterval(options.KeepAliveInterval.Value);
if (options.ServerTimeout.HasValue)
builder.WithServerTimeout(options.ServerTimeout.Value);
if (options.UseAutomaticReconnect)
builder.WithAutomaticReconnect();
if (options.UseStatefulReconnect)
builder.WithStatefulReconnect();
return builder;
}
}