ASP.NET Boilerplate的SignalR集成

问题描述 投票:1回答:1

我正在开发一个ASP.NET Boilerplate项目。我想将SignalR正确地集成到这个项目中。

有文档,但我想知道从后端到前端发送通知时SignalR的流程。任何示例代码或建议都表示赞赏。

signalr aspnetboilerplate
1个回答
4
投票

ASP.NET Boilerplate是开源的,因此您可以查看GitHub上的代码。

来自documentation:Abp.AspNetCore.SignalR包实现了IRealTimeNotifier。

下面的代码适用于ASP.NET Core和jQuery版本,但其他版本的流程是相同的。

  1. 在后端,NotificationDistributer注入和calls IRealTimeNotifierawait RealTimeNotifier.SendNotificationsAsync(userNotifications.ToArray());
  2. SignalRRealTimeNotifier implements SendNotificationsAsync,它获取每个用户的在线客户端,并使用userNotification作为参数调用客户端方法: var onlineClients = _onlineClientManager.GetAllByUserId(userNotification); foreach (var onlineClient in onlineClients) { var signalRClient = _hubContext.Clients.Client(onlineClient.ConnectionId); signalRClient.InvokeAsync("getNotification", userNotification); }
  3. Signalz客户registersgetNotification并以'abp.notifications.received'为参数触发notificationconnection.on('getNotification', function (notification) { abp.event.trigger('abp.notifications.received', notification); });
  4. module-zero-core-template有一个main.js,registers是一个通知处理程序: abp.event.on('abp.notifications.received', function (userNotification) { abp.notifications.showUiNotifyForUserNotification(userNotification); }
  5. showUiNotifyForUserNotification formats and shows关于前端的通知: abp.notifications.showUiNotifyForUserNotification = function (userNotification, options) { var message = abp.notifications.getFormattedMessageFromUserNotification(userNotification); var uiNotifyFunc = abp.notifications.getUiNotifyFuncBySeverity(userNotification.notification.severity); uiNotifyFunc(message, undefined, options); }
© www.soinside.com 2019 - 2024. All rights reserved.