我无法从控制器向特定用户发送通知消息。
我的控制器:
public class SomeController : ControllerBase
{
private readonly IHubContext<NotificationHub> _hubContext;
public SomeController(IHubContext<NotificationHub> hubContext)
{
_hubContext = hubContext;
}
public async Task SomeMethod()
{
//it works
await _hubContext.Clients.All.SendAsync("sendMessage", msg);
//it doesn't work
await _hubContext.Clients.User(userId).SendAsync("sendMessage", msg);
//it also doesn't work
await _hubContext.Clients.Client(userId).SendAsync("sendMessage", msg);
}
}
不幸的是,IHubContext有一些缺陷。在“客户端”属性中,仅“客户端”可用。全部-也就是说,我们只能将消息发送给所有客户端,并且在集线器类中没有诸如“其他”或“呼叫者”之类的属性。另外,我们无法从IHubContext获取连接ID。
还有其他方法可以做到吗?还是只有SQL依赖才有可能?