用于ASP.NET样板的SignalR集成

Sha*_*ika 1 signalr aspnetboilerplate

我正在研究一个ASP.NET Boilerplate项目。我想将SignalR正确集成到该项目中。

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

aar*_*ron 5

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

文档Abp.AspNetCore.SignalR包实现IRealTimeNotifier

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

  1. 在后端,NotificationDistributer注入和调用 IRealTimeNotifier

    await RealTimeNotifier.SendNotificationsAsync(userNotifications.ToArray());
    
    Run Code Online (Sandbox Code Playgroud)
  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);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 所述SignalR客户端的寄存器getNotification和触发器'abp.notifications.received'notification作为参数:

    connection.on('getNotification', function (notification) {
        abp.event.trigger('abp.notifications.received', notification);
    });
    
    Run Code Online (Sandbox Code Playgroud)
  4. 模块零核模板具有main.js注册一个通知处理程序:

    abp.event.on('abp.notifications.received', function (userNotification) {
        abp.notifications.showUiNotifyForUserNotification(userNotification);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  5. 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)