Sha*_*ika 1 signalr aspnetboilerplate
我正在研究一个ASP.NET Boilerplate项目。我想将SignalR正确集成到该项目中。
有文档,但是当我从后端向前端发送通知时,我想了解SignalR的流程。任何示例代码或建议,表示赞赏。
ASP.NET Boilerplate是开源的,因此您可以在GitHub上查看代码。
从文档:Abp.AspNetCore.SignalR包实现IRealTimeNotifier。
下面的代码适用于ASP.NET Core和jQuery版本,但其他版本的流程相同。
在后端,NotificationDistributer注入和调用 IRealTimeNotifier:
await RealTimeNotifier.SendNotificationsAsync(userNotifications.ToArray());
Run Code Online (Sandbox Code Playgroud)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);
}
Run Code Online (Sandbox Code Playgroud)所述SignalR客户端的寄存器对getNotification和触发器'abp.notifications.received'与notification作为参数:
connection.on('getNotification', function (notification) {
abp.event.trigger('abp.notifications.received', notification);
});
Run Code Online (Sandbox Code Playgroud)abp.event.on('abp.notifications.received', function (userNotification) {
abp.notifications.showUiNotifyForUserNotification(userNotification);
}
Run Code Online (Sandbox Code Playgroud)showUiNotifyForUserNotification 格式化并在前端显示通知:
abp.notifications.showUiNotifyForUserNotification = function (userNotification, options) {
var message = abp.notifications.getFormattedMessageFromUserNotification(userNotification);
var uiNotifyFunc = abp.notifications.getUiNotifyFuncBySeverity(userNotification.notification.severity);
uiNotifyFunc(message, undefined, options);
}
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
1058 次 |
| 最近记录: |