현재 내 웹 API 끝점을 호출하는 클라이언트를 만드는 서버 측 웹 응용 프로그램이 있었고 적절하게 웹 응용 프로그램(클라이언트)에 메시지를 다시 게시합니다.
우체부를 사용하여 웹 API 엔드포인트를 트리거할 수 있다는 생각이 있습니다. 그러면 연결된 모든 클라이언트가 이 작업이 실행되고 있다는 메시지를 받게 됩니다.
내 엔드포인트
public class DailyPostingController : ControllerBase
{
private readonly IMapper _mapper;
private readonly IDailyPostingService _DailyPostingService;
private readonly ILogger<DailyPostingController> _logger;
public DailyPostingController(IMapper mapper,
IDailyPostingService DailyPostingService,
ILogger<DailyPostingController> logger)
{
_mapper= mapper;
_DailyPostingService= DailyPostingService;
_logger= logger;
}
[Authorize(Policy= Permissions.DailyPosting.Execute)]
[HttpPost("Execute")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<int> Execute(OperatingCentreQueryModel ocQueryModel)
{
_logger.LogDebug("Executing Daily posting for date#"
+ ocQueryModel.ProcessDate.ToString("dd/mmm/yyy"));
//How to trigger the hub to tell connected clients this process is started?
var res= await _DailyPostingService.Execute(ocQueryModel);
//How to trigger the hub to tell connected clients this process was done?
return res;
}
}
내 허브
public class NotificationHub : Hub<INotificationHub>{
private readonly UsersState _usersState;
private readonly ProcessState _processState;
private readonly ILogger<NotificationHub> _logger;
public NotificationHub(UsersState usersState, ProcessState processState,
ILogger<NotificationHub> logger)
{
_usersState= usersState;
_processState= processState;
_logger= logger;
}
public async Task SendMessage(string user, string message)
{
await Clients.All.ReceiveMessage(user, message);
}
public async Task SendMessageToCaller(string user, string message)
{
await Clients.Caller.ReceiveMessage(user, message);
}
public async Task SendMessageToGroup(string user, string group, string message)
{
await Clients.Group(group).ReceiveMessage(user, message);
}
public override Task OnConnectedAsync()
{
LogDebug($"({Context.ConnectionId}) connected to notification hub.");
return base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
LogDebug($"({Context.ConnectionId}) disconnected to notification hub.");
_usersState.RemoveConnectionId(Context.ConnectionId,
async (user)=> {
await RemoveUser(user);
await RemoveUserProcess(user);
});
await base.OnDisconnectedAsync(exception);
}
private void LogDebug(string msg)
{
_logger.LogDebug("*** " + msg);
}
- 답변 # 1
- 답변 # 2
문서는 이 작업을 수행하는 방법을 설명합니다.
IHubContext 서비스를 사용하여 앱의 다른 위치에서 SignalR 메시지를 보낼 수 있습니다. 필요한 곳에 주입하기만 하면 됩니다.
귀하의 문제를 해결하기 위해 INotificationHub에 새 메서드를 추가합니다.
허브 기능을 사용해야 하는 곳에 강력한 형식의 허브 컨텍스트를 삽입하세요.
엔드포인트는 다음과 같을 수 있습니다.
그런 다음 클라이언트 측에서 "ExcecutingDailyPost" 채널을 듣고 작업에 따라 원하는 대로 해야 합니다.