【发布时间】:2020-08-06 19:48:57
【问题描述】:
是否可以仅通过IHubContext<> 向一个客户发送 SignalR 通知?我有一个带有 JWT 的 API。我有一个 PostsController 将一些“帖子”保存到数据库中。我想要实现的是在帖子添加到数据库后向帖子创建者的所有关注者发送通知。我知道我可以通过在我的 JS / Xamarin 应用程序上调用 HubConnection 来做到这一点,但有没有可能通过 IHubContext<> 来做到这一点?
[Route("api/posts")]
[ApiController]
public class PostsController : ControllerBase
{
private readonly IHubContext<MyHub> _hubContext;
private readonly IMyService _myService;
private readonly IUserService _userService;
public PostsController(
IHubContext<MyHub> hubContext,
IMyService myService,
IUserService userService
)
{
_hubContext= hubContext;
_myService = myService;
_userService = userService;
}
[HttpPost]
[Authorize]
public IActionResult Callback([FromBody] Post post)
{
_myService.Insert(post);
var followers = _userService.GetFollowers(post.UserId);
foreach (var item in followers)
//_hubContext.Client...
return Ok();
}
}
【问题讨论】:
标签: asp.net-core .net-core signalr asp.net-core-webapi signalr-hub