signalR 从服务器端的事件调用

signalR invoke from event in server-side

我创建了一个与 signalR 一起使用的通知系统,还创建了一个在添加通知后立即发生的事件。我想从同一事件中调用 notifictionHub 中的函数 我看到一些解决方案对我正在使用的版本无效,我想知道我该怎么做?

通知中心:

        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...  
        }

在你的活动中class:

//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);
}