【发布时间】:2022-02-14 14:14:47
【问题描述】:
目前我有我的服务器端 Web 应用程序来创建一个客户端来调用我的 Web API 端点,它将消息适当地发布回 Web 应用程序(客户端)。
我的想法是,是否可以使用 Postman 触发 Web 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);
}
【问题讨论】:
标签: signalr asp.net-core-webapi signal-processing signalr-hub