我正在开发一个ASP.NET Boilerplate项目。我想将SignalR正确地集成到这个项目中。
有文档,但我想知道从后端到前端发送通知时SignalR的流程。任何示例代码或建议都表示赞赏。
ASP.NET Boilerplate是开源的,因此您可以查看GitHub上的代码。
来自documentation:Abp.AspNetCore.SignalR包实现了IRealTimeNotifier。
下面的代码适用于ASP.NET Core和jQuery版本,但其他版本的流程是相同的。
NotificationDistributer
注入和calls IRealTimeNotifier
:
await RealTimeNotifier.SendNotificationsAsync(userNotifications.ToArray());
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);
}
getNotification
并以'abp.notifications.received'
为参数触发notification
:
connection.on('getNotification', function (notification) {
abp.event.trigger('abp.notifications.received', notification);
});
abp.event.on('abp.notifications.received', function (userNotification) {
abp.notifications.showUiNotifyForUserNotification(userNotification);
}
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);
}