SignalR basics

This commit is contained in:
Adam 2024-11-14 23:14:35 +01:00
parent a922f6ce39
commit 01bd971a80
9 changed files with 129 additions and 37 deletions

View File

@ -64,6 +64,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Controllers
if (viewModel.IsActive == true)
{
await _logger.InformationAsync($"sending announcements");
await _announcementHubContext.Clients.All.SendAsync("send", viewModel.Body.ToString());
}
return RedirectToAction("AnnouncementList");

View File

@ -5,6 +5,7 @@ using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Data;
using Nop.Plugin.AuctionPlugin.Components;
//using Nop.Plugin.Misc.AuctionPlugin.Components;
using Nop.Plugin.Widgets.AuctionPlugin.Components;
using Nop.Services.Catalog;
using Nop.Services.Cms;
@ -109,6 +110,11 @@ namespace Nop.Plugin.Misc.AuctionPlugin
return typeof(AuctionAdminViewComponent);
}
if (widgetZone.Equals(PublicWidgetZones.HeaderAfter))
{
return typeof(LiveAnnouncementViewComponent);
}
return typeof(AuctionViewComponent);
}
@ -117,7 +123,8 @@ namespace Nop.Plugin.Misc.AuctionPlugin
return Task.FromResult<IList<string>>(new List<string>
{
PublicWidgetZones.ProductPriceTop,
PublicWidgetZones.ProductDetailsBottom,
PublicWidgetZones.ProductDetailsBottom,
PublicWidgetZones.HeaderAfter
//AdminWidgetZones.OrderBillingAddressDetailsBottom,
//AdminWidgetZones.OrderShippingAddressDetailsBottom

View File

@ -1,28 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using Nop.Web.Framework.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Nop.Plugin.Misc.AuctionPlugin.Components
{
[ViewComponent(Name = "LiveAnnouncementView")]
public class AnnouncementViewComponent : NopViewComponent
{
public IViewComponentResult Invoke(string widgetZone, object additionalData)
{
return View("~/Plugins/Misc.AuctionPlugin/Views/LiveAnnouncementView/LiveAnnouncement.cshtml");
}
}
}

View File

@ -0,0 +1,50 @@
using Microsoft.AspNetCore.Mvc;
using Nop.Core;
using Nop.Plugin.Misc.AuctionPlugin;
using Nop.Services.Cms;
using Nop.Services.Logging;
using Nop.Web.Framework.Components;
using Nop.Web.Framework.Infrastructure;
namespace Nop.Plugin.AuctionPlugin.Components
{
[ViewComponent(Name = "LiveAnnouncement")]
public class LiveAnnouncementViewComponent : NopViewComponent
{
protected readonly ILogger _logger;
protected readonly IWorkContext _workContext;
protected readonly IWidgetPluginManager _widgetPluginManager;
public LiveAnnouncementViewComponent(ILogger logger, IWorkContext workContext, IWidgetPluginManager widgetPluginManager)
{
_logger = logger;
_workContext = workContext;
_widgetPluginManager = widgetPluginManager;
}
public async Task<IViewComponentResult> InvokeAsync(string widgetZone, object additionalData)
{
await _logger.InformationAsync("SignalR Widget called");
//ensure that what3words widget is active and enabled
var customer = await _workContext.GetCurrentCustomerAsync();
await _logger.InformationAsync($"SignalR Widget called customer: {customer.Email}");
if (!await _widgetPluginManager.IsPluginActiveAsync(AuctionDefaults.SystemName, customer))
return Content(string.Empty);
await _logger.InformationAsync("SignalR Widget: widget active");
if (!widgetZone.Equals(PublicWidgetZones.HeaderAfter))
{
return Content(string.Empty);
}
return View("~/Plugins/Misc.AuctionPlugin/Views/LiveAnnouncement.cshtml");
}
}
}

View File

@ -1,6 +1,8 @@
$(function () {
console.log("signalRJs Starts");
var connection = new signalR.HubConnectionBuilder()
.withUrl('/announcement')
.withUrl('/auctionhub')
.build();
connection.on('send', data => {
@ -25,6 +27,7 @@
function showannouncement(announcemant) {
console.log("announcement arrived!");
if (announcemant) {
toastr.options = {
"closeButton": true,

View File

@ -2,14 +2,39 @@
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;
using Nop.Services.Logging;
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
{
public class AuctionHub : Hub<IAuctionHubClient>
{
{
ILogger _logger;
//HubCallerContext _hubCallerContext;
public AuctionHub(ILogger logger)
{
_logger = logger;
}
public override async Task OnConnectedAsync()
{
//await _logger.InformationAsync($"Caller connected: id{_hubCallerContext.ConnectionId}");
var userId = Context.ConnectionId;
await _logger.InformationAsync($"Caller connected with id: {userId}");
var userName = Context.GetHttpContext().Request.Query["ConnectionId"];
if (!string.IsNullOrEmpty(userName))
{
await _logger.InformationAsync($"Caller connected with name: {userName}");
}
await base.OnConnectedAsync();
}
public async Task ReceiveMessageFromClient(string message)
{
await _logger.InformationAsync(message);
// Broadcast the message received from the client to all clients
Console.Write($"Received message: {message}");
await Clients.All.SendAsync("Send", message);
@ -18,7 +43,8 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
public async Task Send(string announcement)
{
await Clients.All.SendAsync("Send", announcement);
await _logger.InformationAsync($" Hub Send method called with messgae: {announcement}");
await Clients.All.SendAsync("send", announcement);
}
public async Task SendPriceToUsers(string message)

View File

@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Nop.Core;
@ -50,7 +51,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Infrastructure
services.AddScoped<IAuctionService, AuctionService>();
services.AddScoped<IProductAttributeService, ProductAttributeService>();
services.AddScoped<UrlHelperFactory>();
services.AddScoped<IActionContextAccessor, ActionContextAccessor>();
services.AddScoped<IActionContextAccessor, ActionContextAccessor>();
}
/// <summary>

View File

@ -97,6 +97,9 @@
</ItemGroup>
<ItemGroup>
<None Update="Content\Css\toastr.min.css">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Content\Js\LiveAnnouncement.js">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>

View File

@ -10,12 +10,41 @@
ISettingService _settingContext = EngineContext.Current.Resolve<ISettingService>();
IStoreContext _storeContext = EngineContext.Current.Resolve<IStoreContext>();
Html.AddScriptParts("~/Plugins/Misc.AuctionPlugin/Content/Js/signalr.js");
Html.AddScriptParts("~/Plugins/Misc.AuctionPlugin/Content/Js/LiveAnnouncement.js");
Html.AddCssFileParts("~/Plugins/Misc.AuctionPlugin/Content/Css/toastr.min.css");
Html.AddScriptParts("~/Plugins/Misc.AuctionPlugin/Content/Js/toastr.js");
NopHtml.AddScriptParts(ResourceLocation.Head, "~/Plugins/Misc.AuctionPlugin/Content/Js/signalr.js");
NopHtml.AddScriptParts(ResourceLocation.Footer, "~/Plugins/Misc.AuctionPlugin/Content/Js/LiveAnnouncement.js");
NopHtml.AddCssFileParts("~/Plugins/Misc.AuctionPlugin/Content/Css/toastr.min.css");
NopHtml.AddScriptParts(ResourceLocation.Footer, "~/Plugins/Misc.AuctionPlugin/Content/Js/toastr.js");
}
@* <button id="showToast">Show Toast</button> *@
@* <script>
// Basic Toastr Test
$(document).ready(function () {
toastr.options = {
"closeButton": true,
"debug": false,
"newestOnTop": false,
"progressBar": true,
"positionClass": "toast-bottom-right",
"preventDuplicates": false,
"showDuration": "300",
"hideDuration": "1000",
"timeOut": "50000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
};
// Show toast on button click
$('#showToast').click(function () {
toastr.warning('This is a test notification!');
});
});
</script> *@
<div class="announcementPage">
</div>