using AyCode.Core.Enums; using AyCode.Core.Helpers; using AyCode.Core.Loggers; using AyCode.Entities.LogItems; using AyCode.Services.Loggers; using AyCode.Services.SignalRs; using MessagePack; using Microsoft.AspNetCore.SignalR.Client; using Microsoft.Extensions.DependencyInjection; using TIAMWebApp.Shared.Application.Models.ClientSide; using TIAMWebApp.Shared.Application.Utility; namespace TIAMWebApp.Shared.Application.Services { public class DevAdminSignalClient(IEnumerable logWriters) : AcSignalRClientBase("DevAdminHub", logWriters); public abstract class AcSignalRClientBase : IAcSignalRHubClient { protected readonly HubConnection HubConnection; protected readonly LoggerClient Logger; protected AcSignalRClientBase(string hubName, IEnumerable logWriters) { Logger = new LoggerClient(GetType().Name, logWriters.ToArray()); HubConnection = new HubConnectionBuilder() .WithUrl($"{Setting.BaseUrl}/{hubName}") //.AddMessagePackProtocol(options => options.SerializerOptions = MessagePackSerializerOptions.Standard.WithSecurity(MessagePackSecurity.UntrustedData)) .Build(); HubConnection.On("Post", Post); HubConnection.StartAsync().Forget(); } public async Task StartConnection() { if (HubConnection.State == HubConnectionState.Disconnected) await HubConnection.StartAsync(); if (HubConnection.State != HubConnectionState.Connected) await TaskHelper.WaitToAsync(() => HubConnection.State == HubConnectionState.Connected, 3000, 100); } public async Task StopConnection() { await HubConnection.StopAsync(); await HubConnection.DisposeAsync(); } public virtual async Task Get(string user, int messageTag) { Logger.Info($"{nameof(user)}: {user}; {nameof(messageTag)}: {messageTag}"); await StartConnection(); await HubConnection.SendAsync("Get", "", SignalRTags.GetTransfersAsync); } public virtual async Task Post(string user, int messageTag, object? message) { Logger.Info($"{nameof(user)}: {user}; {nameof(messageTag)}: {messageTag}"); if (messageTag == SignalRTags.PostTransfersAsync) { return; } await StartConnection(); await HubConnection.SendAsync("Post", SignalRTags.PostTransfersAsync, message); } } }