2017-12-29 3 views
2

저는 ASP.NET Boilerplate 프로젝트를 진행하고 있습니다. SignalR을이 프로젝트에 알맞게 통합하고 싶습니다.ASP.NET 용 SignalR 통합 Boilerplate

문서가 있지만 백엔드에서 프런트 엔드로 알림을 보낼 때 SignalR의 흐름을 알고 싶습니다. 모든 샘플 코드 또는 제안은 높이 평가됩니다.

답변

2

ASP.NET Boilerplate는 오픈 소스이기 때문에 코드는 GitHub에서 확인할 수 있습니다. documentation에서

: Abp.AspNetCore.SignalR 패키지는 IRealTimeNotifier을 구현합니다.

아래의 코드는 ASP.NET 코어 & jQuery 버전이지만 다른 버전에서는 흐름이 동일합니다. 백 엔드에

  1. , NotificationDistributer를 분사하고 callsIRealTimeNotifier : 각 사용자에 대한 온라인 클라이언트를 가져오고와 클라이언트 측 메소드를 호출
  2. SignalRRealTimeNotifierimplementsSendNotificationsAsync

    await RealTimeNotifier.SendNotificationsAsync(userNotifications.ToArray()); 
    
    , userNotification을 매개 변수로 사용하십시오 :

    var onlineClients = _onlineClientManager.GetAllByUserId(userNotification); 
    foreach (var onlineClient in onlineClients) 
    { 
        var signalRClient = _hubContext.Clients.Client(onlineClient.ConnectionId); 
        signalRClient.InvokeAsync("getNotification", userNotification); 
    } 
    
  3. getNotification위한 SignalR 클라이언트 registers 및 파라미터로 notification으로 'abp.notifications.received' 트리거 :

    abp.event.on('abp.notifications.received', function (userNotification) { 
        abp.notifications.showUiNotifyForUserNotification(userNotification); 
    } 
    
  4. :

    connection.on('getNotification', function (notification) { 
        abp.event.trigger('abp.notifications.received', notification); 
    }); 
    
  5. module-zero-core-templatemain.js을 갖는다 registers 통지 핸들러 그

    showUiNotifyForUserNotification프런트 엔드에3210 알림 : 자세한 솔루션 아론

    abp.notifications.showUiNotifyForUserNotification = function (userNotification, options) { 
        var message = abp.notifications.getFormattedMessageFromUserNotification(userNotification); 
        var uiNotifyFunc = abp.notifications.getUiNotifyFuncBySeverity(userNotification.notification.severity); 
        uiNotifyFunc(message, undefined, options); 
    } 
    
+0

감사합니다. 이것은 효과가있다. – Shashika