ASP.NET 样板的 SignalR 集成
SignalR integration for ASP.NET Boilerplate
我正在处理 ASP.NET 样板项目。我想将 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
的 SignalR 客户端 registers 并使用 notification
作为参数触发 'abp.notifications.received'
:
connection.on('getNotification', function (notification) {
abp.event.trigger('abp.notifications.received', notification);
});
module-zero-core-template has main.js that registers一个通知处理程序:
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);
}
我正在处理 ASP.NET 样板项目。我想将 SignalR 正确地集成到这个项目中。
有文档,但我想知道SignalR从后端向前端发送通知时的流程。感谢任何示例代码或建议。
ASP.NET Boilerplate 是开源的,所以你可以在 GitHub.
上查看代码来自 documentation:Abp.AspNetCore.SignalR 包实现 IRealTimeNotifier.
以下代码适用于 ASP.NET Core & jQuery 版本,但其他版本的流程相同。
在后端,
NotificationDistributer
注入和 callsIRealTimeNotifier
:await RealTimeNotifier.SendNotificationsAsync(userNotifications.ToArray());
SignalRRealTimeNotifier
implementsSendNotificationsAsync
,获取每个用户的在线客户端,并调用客户端方法userNotification
作为参数:var onlineClients = _onlineClientManager.GetAllByUserId(userNotification); foreach (var onlineClient in onlineClients) { var signalRClient = _hubContext.Clients.Client(onlineClient.ConnectionId); signalRClient.InvokeAsync("getNotification", userNotification); }
getNotification
的 SignalR 客户端 registers 并使用notification
作为参数触发'abp.notifications.received'
:connection.on('getNotification', function (notification) { abp.event.trigger('abp.notifications.received', notification); });
module-zero-core-template has main.js that registers一个通知处理程序:
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); }