MessageWrapper SenderId added, Data sent as JSON string, serialized as JSON in client and in server
This commit is contained in:
parent
f938fc8eb2
commit
2c0e32b093
|
|
@ -76,7 +76,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Controllers
|
||||||
{
|
{
|
||||||
Title = viewModel.Name,
|
Title = viewModel.Name,
|
||||||
Message = viewModel.Body,
|
Message = viewModel.Body,
|
||||||
}
|
}.ToJson()
|
||||||
};
|
};
|
||||||
|
|
||||||
var jsonMessage = JsonConvert.SerializeObject(announcement, Formatting.Indented, new JsonSerializerSettings
|
var jsonMessage = JsonConvert.SerializeObject(announcement, Formatting.Indented, new JsonSerializerSettings
|
||||||
|
|
@ -121,7 +121,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Controllers
|
||||||
ProductName = viewModel.ProductName,
|
ProductName = viewModel.ProductName,
|
||||||
BidPrice = viewModel.BidPrice.ToString(),
|
BidPrice = viewModel.BidPrice.ToString(),
|
||||||
NextStepAmount = viewModel.NextStepAmount.ToString()
|
NextStepAmount = viewModel.NextStepAmount.ToString()
|
||||||
}
|
}.ToJson()
|
||||||
};
|
};
|
||||||
var jsonMessage = JsonConvert.SerializeObject(bid, Formatting.Indented,
|
var jsonMessage = JsonConvert.SerializeObject(bid, Formatting.Indented,
|
||||||
new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }
|
new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ using Nop.Data;
|
||||||
using Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Components;
|
using Nop.Plugin.Misc.AuctionPlugin.Areas.Admin.Components;
|
||||||
using Nop.Plugin.Misc.AuctionPlugin.Components;
|
using Nop.Plugin.Misc.AuctionPlugin.Components;
|
||||||
|
|
||||||
|
|
||||||
//using Nop.Plugin.Misc.AuctionPlugin.Components;
|
//using Nop.Plugin.Misc.AuctionPlugin.Components;
|
||||||
using Nop.Services.Catalog;
|
using Nop.Services.Catalog;
|
||||||
using Nop.Services.Cms;
|
using Nop.Services.Cms;
|
||||||
|
|
|
||||||
|
|
@ -24,9 +24,10 @@ $(function () {
|
||||||
start();
|
start();
|
||||||
});
|
});
|
||||||
|
|
||||||
window.sendMessageToServer = function (messageType, data) {
|
window.sendMessageToServer = function (messageType, senderId, data) {
|
||||||
var messageWrapper = {
|
var messageWrapper = {
|
||||||
MessageType: messageType,
|
MessageType: messageType,
|
||||||
|
SenderId: senderId,
|
||||||
Data: data
|
Data: data
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,8 @@
|
||||||
let animation = "slideDown";
|
let animation = "slideDown";
|
||||||
const handlers = {
|
const handlers = {
|
||||||
announcement: function (data) {
|
announcement: function (data) {
|
||||||
toastr.info(`<div class="item announcemantToast">${data.message}</div>`, data.title, {
|
var myObject = JSON.parse(data);
|
||||||
|
toastr.info(`<div class="item announcemantToast">${myObject.message}</div>`, myObject.title, {
|
||||||
"closeButton": true,
|
"closeButton": true,
|
||||||
"positionClass": "toast-bottom-right",
|
"positionClass": "toast-bottom-right",
|
||||||
"newestOnTop": true,
|
"newestOnTop": true,
|
||||||
|
|
@ -22,7 +23,9 @@
|
||||||
$('.toast-info').css("background-color", "#008080");
|
$('.toast-info').css("background-color", "#008080");
|
||||||
},
|
},
|
||||||
bidNotification: function (data) {
|
bidNotification: function (data) {
|
||||||
toastr.success(`<div class="item bidToast"><p>${data.bidPrice}</p><p>${data.productName}</p></div>`, "New bid arrived", {
|
console.log(data);
|
||||||
|
var myObject = JSON.parse(data);
|
||||||
|
toastr.success(`<div class="item bidToast"><p>${myObject.bidPrice}</p><p>${myObject.productName}</p></div>`, "New bid arrived", {
|
||||||
"closeButton": true,
|
"closeButton": true,
|
||||||
"positionClass": "toast-bottom-right",
|
"positionClass": "toast-bottom-right",
|
||||||
"newestOnTop": true,
|
"newestOnTop": true,
|
||||||
|
|
@ -43,39 +46,36 @@
|
||||||
var publicProductBidBox = document.getElementById("publicProductBidBox");
|
var publicProductBidBox = document.getElementById("publicProductBidBox");
|
||||||
if (publicProductBidBox)
|
if (publicProductBidBox)
|
||||||
{
|
{
|
||||||
refreshPublicBidBox(data);
|
refreshPublicBidBox(myObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
auctionUpdateAlternate: function (data) {
|
openItemMessage: function (data) {
|
||||||
const widgetPriceElement = document.getElementById("WidgetPrice");
|
var myObject = JSON.parse(data);
|
||||||
|
toastr.success(`<div class="item bidToast"><p>${myObject.bidPrice}</p><p>${myObject.productName}</p></div>`, "Item auction is OPENED!", {
|
||||||
|
"closeButton": true,
|
||||||
|
"positionClass": "toast-bottom-right",
|
||||||
|
"newestOnTop": true,
|
||||||
|
"progressBar": true,
|
||||||
|
"preventDuplicates": false,
|
||||||
|
"onclick": null,
|
||||||
|
"showDuration": "30000",
|
||||||
|
"hideDuration": "1000",
|
||||||
|
"timeOut": "5000",
|
||||||
|
"extendedTimeOut": "1000",
|
||||||
|
"showEasing": "swing",
|
||||||
|
"hideEasing": "linear",
|
||||||
|
"showMethod": animation,
|
||||||
|
"hideMethod": "fadeOut"
|
||||||
|
});
|
||||||
|
$('.toast-success').css("background-color", "#4caf50");
|
||||||
|
|
||||||
if (widgetPriceElement) {
|
var publicProductBidBox = document.getElementById("publicProductBidBox");
|
||||||
widgetPriceElement.textContent = data.bidPrice; // Update the price
|
if (publicProductBidBox) {
|
||||||
console.log(`WidgetPrice updated to: ${data.bidPrice}`);
|
refreshPublicBidBox(myObject);
|
||||||
} else {
|
|
||||||
console.warn("Element with ID 'WidgetPrice' not found in the DOM.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
//auctionUpdate: function (data) {
|
|
||||||
// // Refresh the ViewComponent using AJAX
|
|
||||||
// $.ajax({
|
|
||||||
// url: '/Auction/RefreshAuctionWidget', // Controller endpoint
|
|
||||||
// type: 'POST',
|
|
||||||
// contentType: 'application/json',
|
|
||||||
// data: JSON.stringify({
|
|
||||||
// WidgetZone: data.WidgetZone,
|
|
||||||
// ProductId: data.ProductId
|
|
||||||
// }),
|
|
||||||
// success: function (response) {
|
|
||||||
// //$('#auction-widget').html(response); // Update the DOM element
|
|
||||||
// console.log("Auction widget refreshed!");
|
|
||||||
// },
|
|
||||||
// error: function (error) {
|
|
||||||
// console.error("Error refreshing auction widget:", error);
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
//},
|
|
||||||
|
|
||||||
// Add more handlers as needed
|
// Add more handlers as needed
|
||||||
default: function (data) {
|
default: function (data) {
|
||||||
|
|
@ -87,10 +87,13 @@
|
||||||
function messageRouter(message) {
|
function messageRouter(message) {
|
||||||
// Parse the JSON message
|
// Parse the JSON message
|
||||||
try {
|
try {
|
||||||
const parsedMessage = JSON.parse(message);
|
var parsedMessage = JSON.parse(message);
|
||||||
const messageType = parsedMessage.messageType;
|
var messageType = parsedMessage.messageType;
|
||||||
const messageData = parsedMessage.data;
|
var senderId = parsedMessage.senderId;
|
||||||
|
var messageData = parsedMessage.data;
|
||||||
console.log("Message type:" + messageType);
|
console.log("Message type:" + messageType);
|
||||||
|
console.log("Message sender:" + senderId);
|
||||||
|
console.log("Message content" + parsedMessage.data);
|
||||||
// Route to appropriate handler, default if no match
|
// Route to appropriate handler, default if no match
|
||||||
(handlers[messageType] || handlers.default)(messageData);
|
(handlers[messageType] || handlers.default)(messageData);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages
|
||||||
public class MessageWrapper
|
public class MessageWrapper
|
||||||
{
|
{
|
||||||
public string MessageType { get; set; }
|
public string MessageType { get; set; }
|
||||||
public object Data { get; set; }
|
public int SenderId { get; set; }
|
||||||
|
public string Data { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos;
|
||||||
|
|
||||||
|
namespace Nop.Plugin.Misc.AuctionPlugin.Models
|
||||||
|
{
|
||||||
|
public class OpenItemRequest : AuctionBidDto
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -11,6 +11,7 @@ using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
|
||||||
using Newtonsoft.Json.Serialization;
|
using Newtonsoft.Json.Serialization;
|
||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
using Nop.Core;
|
using Nop.Core;
|
||||||
|
using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums;
|
||||||
|
|
||||||
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
{
|
{
|
||||||
|
|
@ -49,7 +50,11 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
switch (message.MessageType)
|
switch (message.MessageType)
|
||||||
{
|
{
|
||||||
case "BidRequestMessage":
|
case "BidRequestMessage":
|
||||||
await HandleBidRequest(message.Data.ToString()?.JsonTo<AuctionBidRequest>());
|
await HandleBidRequest(message.SenderId, message.Data.JsonTo<AuctionBidRequest>());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "OpenItemRequestMessage":
|
||||||
|
await HandleOpenItemMessageRequest(message.SenderId, message.Data.JsonTo<OpenItemRequest>());
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Add other message types here
|
// Add other message types here
|
||||||
|
|
@ -59,7 +64,37 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task HandleBidRequest(AuctionBidRequest bidRequestMessage)
|
private async Task HandleOpenItemMessageRequest(int SenderId, OpenItemRequest openItemMessage)
|
||||||
|
{
|
||||||
|
if (openItemMessage == null)
|
||||||
|
{
|
||||||
|
_logger.Error($"SignalRMessageHandler.HandleOpenItemMessageRequest(); openItemRequestMessage == null");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _logger.InformationAsync($"SignalRMessageHandler.HandleOpenItemMessageRequest(); Item to open: - ProductToAuctionMappingId: {openItemMessage.ProductAuctionMappingId}");
|
||||||
|
|
||||||
|
//get auction
|
||||||
|
var auction = await _auctionService.GetAuctionDtoByProductToAuctionIdAsync(openItemMessage.ProductAuctionMappingId);
|
||||||
|
//get productToAuction
|
||||||
|
var productToAuction = await _auctionService.GetProductToAuctionDtoByIdAsync(openItemMessage.ProductAuctionMappingId);
|
||||||
|
|
||||||
|
auction.Closed = false;
|
||||||
|
|
||||||
|
await _logger.InformationAsync($"SignalRMessageHandler.HandleOpenItemMessageRequest(); Auction {auction.Id}, ProducToAuction {productToAuction.Id}");
|
||||||
|
//_auctionService.UpdateAuctionAsync(auction);
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.Error($"Error {ex.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task HandleBidRequest(int senderId, AuctionBidRequest bidRequestMessage)
|
||||||
{
|
{
|
||||||
if (bidRequestMessage == null)
|
if (bidRequestMessage == null)
|
||||||
{
|
{
|
||||||
|
|
@ -112,12 +147,13 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
|
||||||
var bid = new MessageWrapper
|
var bid = new MessageWrapper
|
||||||
{
|
{
|
||||||
MessageType = "bidNotification",
|
MessageType = "bidNotification",
|
||||||
|
SenderId = senderId,
|
||||||
Data = new BidNotificationMessage
|
Data = new BidNotificationMessage
|
||||||
{
|
{
|
||||||
ProductName = auctionBid.ProductId.ToString(),
|
ProductName = auctionBid.ProductId.ToString(),
|
||||||
BidPrice = auctionBid.BidPrice.ToString(CultureInfo.InvariantCulture),
|
BidPrice = auctionBid.BidPrice.ToString(CultureInfo.InvariantCulture),
|
||||||
NextStepAmount = "50000"
|
NextStepAmount = "50000"
|
||||||
}
|
}.ToJson()
|
||||||
};
|
};
|
||||||
|
|
||||||
await _hubContext.Clients.All.SendAsync("send", bid.ToJson());
|
await _hubContext.Clients.All.SendAsync("send", bid.ToJson());
|
||||||
|
|
|
||||||
|
|
@ -46,10 +46,10 @@
|
||||||
<div id="publicProductBidBoxAdmin" class="p-3 bg-primary text-white">
|
<div id="publicProductBidBoxAdmin" class="p-3 bg-primary text-white">
|
||||||
<h4>Manage auction!</h4>
|
<h4>Manage auction!</h4>
|
||||||
<div id="bidBoxAdminButtons" class="d-flex justify-content-between mb-3">
|
<div id="bidBoxAdminButtons" class="d-flex justify-content-between mb-3">
|
||||||
<button id="signalRBidButton" class="btn btn-scondary" style="text-transform: uppercase;" type="button">
|
<button id="signalROpenItemButton" class="btn btn-secondary" style="text-transform: uppercase;" type="button">
|
||||||
Activate item
|
Activate item
|
||||||
</button>
|
</button>
|
||||||
<button id="signalRBidButton" class="btn btn-warning" style="text-transform: uppercase;" type="button">
|
<button id="signalRFirstWarningButton" class="btn btn-warning" style="text-transform: uppercase;" type="button">
|
||||||
First warning
|
First warning
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -95,12 +95,42 @@
|
||||||
ProductId: pageViewModel.ProductId,
|
ProductId: pageViewModel.ProductId,
|
||||||
CustomerId: pageViewModel.CustomerId
|
CustomerId: pageViewModel.CustomerId
|
||||||
};
|
};
|
||||||
|
var content = JSON.stringify(bidMessage);
|
||||||
sendMessageToServer("BidRequestMessage", bidMessage);
|
sendMessageToServer("BidRequestMessage", pageViewModel.CustomerId, content);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$("#signalROpenItemButton").on("click", function () {
|
||||||
|
|
||||||
|
document.getElementById("signalROpenItemButton").disabled = true;
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
var openItemMessage = {
|
||||||
|
ProductAuctionMappingId: pageViewModel.ProductToAuctions,
|
||||||
|
AdminId: pageViewModel.CustomerId
|
||||||
|
};
|
||||||
|
var content = JSON.stringify(openItemMessage);
|
||||||
|
console.log(content);
|
||||||
|
sendMessageToServer("OpenItemRequestMessage", pageViewModel.CustomerId, content);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
$("#signalRFirstWarningButton").on("click", function () {
|
||||||
|
|
||||||
|
document.getElementById("signalRFirstWarningButton").disabled = true;
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
var itemFirstWarningMessage = {
|
||||||
|
ProductAuctionMappingId: pageViewModel.ProductToAuctions,
|
||||||
|
AdminId: pageViewModel.CustomerId
|
||||||
|
};
|
||||||
|
var content = JSON.stringify(itemFirstWarningMessage);
|
||||||
|
console.log(content);
|
||||||
|
sendMessageToServer("FirstWarningMessage", pageViewModel.CustomerId, content);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue