Mango.Nop.Plugins/Nop.Plugin.Misc.AuctionPlugin/Hubs/AuctionHub.cs

78 lines
3.2 KiB
C#

using System;
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;
using Nop.Services.Logging;
using Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages;
using Nop.Plugin.Misc.AuctionPlugin.Models;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using Nop.Web.Models.Media;
using Nop.Services.Catalog;
using Nop.Plugin.Misc.AuctionPlugin.Services;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
using Newtonsoft.Json.Linq;
using static LinqToDB.Reflection.Methods.LinqToDB.Insert;
using Mango.Nop.Services;
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
{
public class AuctionHub(SessionService sessionService, ILogger logger, SignalRMessageHandler signalRMessageHandler)
: Hub<IAuctionHubClient>
{
//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)
{
//await _logger.InformationAsync($"AuctionHub.OnConnectedAsync(); message: {message}");
if (sessionService.TryGetSessionItem(Context.GetHttpContext().Session.Id, out var sessionItem)) sessionItem.RequestCount++;
Console.WriteLine($"Received message: {message}");
await Clients.All.SendAsync("Send", message);
}
public async Task ReceiveMessageFromClient(MessageWrapper message)
{
// Log the message type and data
//await _logger.InformationAsync($"AuctionHub.OnConnectedAsync(); Received message of type: {message.MessageType}");
if (sessionService.TryGetSessionItem(Context.GetHttpContext().Session.Id, out var sessionItem)) sessionItem.RequestCount++;
await signalRMessageHandler.HandleMessage(message, sessionItem, Context.ConnectionId);
}
}
}