using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.AspNetCore.SignalR.Protocol;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace AyCode.Services.SignalRs;
///
/// Client-side registration extension for the ("acbinary").
/// Mirrors the ASP.NET Core idiomatic pattern of AddJsonProtocol(...) /
/// AddMessagePackProtocol(...).
///
/// For the server-side equivalent see AcSignalRServerProtocolExtensions in
/// AyCode.Services.Server — kept separate to avoid dragging the server SignalR assembly
/// (Microsoft.AspNetCore.SignalR.Core) into pure client projects (MAUI, WASM).
///
///
public static class AcSignalRProtocolExtensions
{
///
/// Registers as the protocol for a client .
/// Call on the during client setup.
///
public static IHubConnectionBuilder AddAcBinaryProtocol(this IHubConnectionBuilder builder, Action? configure = null)
{
builder.Services.AddSingleton(sp => BuildProtocol(sp, configure));
return builder;
}
///
/// Shared factory used by both client (this file) and server
/// (AcSignalRServerProtocolExtensions in AyCode.Services.Server).
/// Resolves options from DI (IOptions<T>), clones them, applies the inline
/// override, validates, and constructs the protocol.
///
public static IHubProtocol BuildProtocol(IServiceProvider sp, Action? configure)
{
var diOptions = sp.GetService>()?.Value;
var options = diOptions?.Clone() ?? new AcBinaryHubProtocolOptions();
options.Logger ??= sp.GetService>();
configure?.Invoke(options);
options.Validate();
return new AyCodeBinaryHubProtocol(options);
}
}