Сигнал вызывается из события на стороне сервера - PullRequest
0 голосов
/ 26 сентября 2019

Я создал систему уведомлений, используемую с signalR, а также создал событие, которое происходит, как только добавляется уведомление.Я хотел бы вызвать функцию в моем notifictionHub из того же события, что я видел некоторые решения, которые не подходят для версии, с которой я работаю, я хотел бы знать, как я могу это сделать?

NotificationHub:

        public async Task SendNotification(string user, string content)
        {
            string userID = "3";
            foreach(var cid in _connections.GetConnections(userID))
            {
                await Clients.Client(cid).SendAsync("ReceiveNotification", user, content);
            }
        }

Добавить событие уведомления:

        private static void Notifications_OnNotificationAdd(Account account, Notification notification)
        {
            // call NotificationHub->SendNotification from here...  
        }

1 Ответ

0 голосов
/ 26 сентября 2019

В вашем классе событий:

//using this instance object, we are able to access and call the hub methods.
private IHubContext<notifictionHub> _hub;

В вашем методе событий:

private static void Notifications_OnNotificationAdd(Account account, Notification notification)
{
   _hub.Clients.All.SendNotification(yourUser, yourContent);
}
...