using BMA.EHR.Application.Repositories; using BMA.EHR.Insignia.Service.Configuration; using SocketIOClient; using System.Security.Claims; namespace BMA.EHR.Insignia.Service.Services; public class InsigniaRequestProcessService : BackgroundService { private readonly IBackgroundTaskQueue _queue; private readonly IHttpContextAccessor _httpContextAccessor; private readonly IServiceScopeFactory _serviceScopeFactory; public InsigniaRequestProcessService( IBackgroundTaskQueue queue, IHttpContextAccessor httpContextAccessor, IServiceScopeFactory serviceScopeFactory) { _queue = queue; _httpContextAccessor = httpContextAccessor; _serviceScopeFactory = serviceScopeFactory; } #region " Properties " protected string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value; protected string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value; protected bool? IsPlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1"); protected string? AccessToken => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"]; #endregion public override async Task StartAsync(CancellationToken cancellationToken) { var client = new SocketIO("wss://hrmsbkk.case-collection.com", new SocketIOOptions { // เพิ่ม token ใน handshake.auth Auth = new { token = AccessToken ?? "" }, Path = "/api/v1/org-socket" }); client.OnConnected += async (sender, e) => { Console.WriteLine("Connected to Socket.IO server"); await client.EmitAsync("eventName", "Hello from .NET client"); }; await client.ConnectAsync(); await base.StartAsync(cancellationToken); } //public override async Task StartAsync(CancellationToken cancellationToken) //{ //var client = new SocketIO("https://bma-ehr.frappet.synology.me/api/v1/org-socket", // new SocketIOOptions // { // // เพิ่ม token ใน handshake.auth // Auth = new { token = AccessToken ?? "" } // }); //client.OnConnected += async (sender, e) => //{ // Console.WriteLine("Connected to Socket.IO server"); // await client.EmitAsync("eventName", "Hello from .NET client"); //}; //await client.ConnectAsync(); //await base.StartAsync(cancellationToken); //} protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { try { var workItem = await _queue.DequeueAsync(stoppingToken); if (workItem != null) { var startTime = DateTime.Now; await workItem(stoppingToken); var endTime = DateTime.Now; var duration = endTime - startTime; // Resolve repository from a scope because DbContext is scoped/transient. using var scope = _serviceScopeFactory.CreateScope(); var userProfileRepository = (UserProfileRepository)scope.ServiceProvider.GetService(typeof(UserProfileRepository)); if (userProfileRepository != null) { await userProfileRepository.PostInsigniaMessageToSocket("Task completed", UserId ?? "", AccessToken); } } } catch (OperationCanceledException) { break; } catch (Exception ex) { // รอสักครู่ก่อนประมวลผล task ถัดไป เพื่อป้องกันการวนลูปข้อผิดพลาด await Task.Delay(1000, stoppingToken); } } } public override async Task StopAsync(CancellationToken cancellationToken) { await base.StopAsync(cancellationToken); } }