我创建了一个与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...
}
答案 0 :(得分:0)
在您的活动课中:
//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);
}