using Microsoft.AspNetCore.SignalR; using Nop.Services.Logging; using Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages; using Nop.Plugin.Misc.AuctionPlugin.Services; namespace Nop.Plugin.Misc.AuctionPlugin.Hubs { public class AuctionHub(SessionService sessionService, ILogger logger, SignalRMessageHandler signalRMessageHandler) : Hub { //HubCallerContext _hubCallerContext; public override async Task OnConnectedAsync() { var connectionId = Context.ConnectionId; //if (sessionService.GetOrCreateSessionItem(connectionId) == null) await logger.ErrorAsync($"AuctionHub.OnConnectedAsync(); (sessionItem == null); connectionId: {connectionId}"); //await _logger.InformationAsync($"AuctionHub.OnConnectedAsync(); Caller connected with id: {connectionId}"); var httpContext = Context.GetHttpContext(); if (httpContext == null) await logger.ErrorAsync($"AuctionHub.OnConnectedAsync(); (httpContext == null); connectionId: {connectionId}"); else { if (sessionService.GetOrCreateSessionItem(httpContext.Session.Id) == null) await logger.ErrorAsync($"AuctionHub.OnConnectedAsync(); (sessionItem == null); connectionId: {connectionId}; sessionId: {httpContext.Session.Id}"); var userName = httpContext.Request.Query["ConnectionId"]; if (!string.IsNullOrEmpty(userName)) { await logger.InformationAsync($"AuctionHub.OnConnectedAsync(); Caller connected with name: {userName}; connectionId: {connectionId}"); } } await base.OnConnectedAsync(); } public override Task OnDisconnectedAsync(Exception exception) { sessionService.TryRemoveSessionItem(Context.ConnectionId, out _); return base.OnDisconnectedAsync(exception); } public async Task ReceiveRegularMessageFromClient(string message) { IncrementRequestCount(); await Clients.All.SendAsync("Send", message); } public async Task ReceiveMessageFromClient(MessageWrapper message) { var sessionItem = IncrementRequestCount(); await signalRMessageHandler.HandleMessage(message, sessionItem, Context.ConnectionId); } private SessionItem IncrementRequestCount() { SessionItem sessionItem = null; var httpContext = Context.GetHttpContext(); if (httpContext != null && sessionService.TryGetSessionItem(httpContext.Session.Id, out sessionItem)) sessionItem.RequestCount++; return sessionItem; } } }