SignalR客户端接收来自仪表板客户端的呼叫,但不接收来自电子商务客户端的呼叫

时间:2019-07-05 10:20:44

标签: c# vue.js asp.net-core signalr asp.net-core-signalr

我有2个Vue.js客户端,它们共享同一个后端服务器(ASP.NET Core)。其中一个客户端是电子商务站点,另一个客户端是管理订单的管理仪表板。

我已实现SignalR以允许在订单仪表板表上实时更新订单。当我在管理信息中心上更新订单状态时,例如,当前更新实时工作,而无需刷新浏览器。当在浏览器1上将“从交货状态”更新为“已交货”时,浏览器2会立即显示更改,而无需刷新页面。

Order updated via dashboard, SignalR event received

但是,当我从电子商务网站创建新订单时,即使我已检查我的代码是否已进入Hub方法以向所有客户广播事件和新订单详细信息,订单面板仍不会更新/刷新,当我检查开发工具时它也不会收到任何事件。我希望在此问题上有所帮助。

mount()中的集线器方法

    // Establish hub connection
    this.connection = await OrderHub.connectToOrderHub();

    // Establish hub methods
    this.connection.on("OneOrder", order => {
      console.log("OneOrder called");
      console.log(order);
      this.getAllOrders();
    });

    this.connection.on("MultipleOrders", orders => {
      console.log("MultipleOrders called");
      console.log(orders);
      this.getAllOrders();
    });

    // start the connection
    this.connection
      .start()
      .then(() => {
        console.log("Connection to hub started");
      })
      .catch(err => console.log(err));

orderHub.js

const signalR = require("@aspnet/signalr");

class OrderHub {
  async connectToOrderHub() {
    return new signalR.HubConnectionBuilder()
      .withUrl("https://localhost:44393/order-hub")
      .configureLogging(signalR.LogLevel.Error)
      .build();
  }
}
export default new OrderHub();

服务器端的OrderHub.cs

public interface IOrderHub
    {
        Task NotifyOneChange(Order newOrder);
        Task NotifyMultipleChanges(List<Order> newOrders);
    }

    public class OrderHub : Hub, IOrderHub
    {
        private readonly IHubContext<OrderHub> _hubContext;

        public OrderHub(IHubContext<OrderHub> hubContext)
        {
            _hubContext = hubContext;
        }

        public async Task NotifyOneChange(Order newOrder)
        {
            await _hubContext.Clients.All.SendAsync("OneOrder", newOrder);
        }

        public async Task NotifyMultipleChanges(List<Order> newOrders)
        {
            await _hubContext.Clients.All.SendAsync("MultipleOrders", newOrders);
        }
    }

创建订单方法

public async Task<Order> Create(Order order)
        {
            Order newOrder;
            try
            {
                List<string> imgKeys = new List<string>();
                foreach (OrderItem item in order.OrderItems)
                {
                    imgKeys.Add(item.OrderImageKey);
                }

                // make images on s3 permanent
                List<string> imgUrls = await _s3Service.CopyImagesAsync(imgKeys);

                // put the new images url into object
                for (int i = 0; i < order.OrderItems.Count; i++)
                {
                    order.OrderItems.ElementAt(i).OrderImageUrl = imgUrls.ElementAt(i);
                }

                // create new order object to be added
                newOrder = new Order()
                {
                    CreatedAt = DateTime.Now,
                    UpdatedAt = DateTime.Now,
                    OrderSubtotal = decimal.Parse(order.OrderSubtotal.ToString()),
                    OrderTotal = decimal.Parse(order.OrderTotal.ToString()),
                    ReferenceNo = order.ReferenceNo,
                    Request = order.Request,
                    Email = EncryptString(order.EmailString, encryptionKey),
                    UpdatedById = order.UpdatedById,
                    DeliveryTypeId = order.DeliveryTypeId,
                    Address = order.Address,
                    StatusId = 1,
                    OrderItems = order.OrderItems
                };

                // add to database
                await _context.Orders.AddAsync(newOrder);
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                throw new AppException("Unable to create product record.", new { message = ex.Message });
            }
            await _orderHub.NotifyOneChange(newOrder); // it steps inside & executes the method, but client side does not receive any events
            // returns product once done
            return newOrder;
        }

1 个答案:

答案 0 :(得分:0)

我设法弄清了这个问题的原因。当邮件超过一定大小时,SignalR似乎断开,此问题引用为here。为了解决这个问题,我决定传递订单的ID而不是整个订单对象,然后执行另一个GET调用以在单独的API调用中检索详细信息,以使SignalR消息保持较小。