You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.3 KiB
C#
47 lines
1.3 KiB
C#
9 months ago
|
using EntrustSettle.Common.Const;
|
||
|
using EntrustSettle.Controllers;
|
||
|
using EntrustSettle.Hubs;
|
||
|
using Microsoft.AspNetCore.Mvc;
|
||
|
using Microsoft.AspNetCore.SignalR;
|
||
|
|
||
|
namespace EntrustSettle.Api.Controllers;
|
||
|
|
||
|
/// <summary>
|
||
|
/// SignalR测试
|
||
|
/// </summary>
|
||
|
[ApiExplorerSettings(GroupName = ApiGroupNameConst.System)]
|
||
|
public class SignalRTestController : BaseApiController
|
||
|
{
|
||
|
private readonly IHubContext<ChatHub, IChatClient> _hubContext;
|
||
|
|
||
|
public SignalRTestController(IHubContext<ChatHub, IChatClient> hubContext)
|
||
|
{
|
||
|
_hubContext = hubContext;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 向指定用户发送消息
|
||
|
/// </summary>
|
||
|
/// <param name="user"></param>
|
||
|
/// <param name="message"></param>
|
||
|
/// <returns></returns>
|
||
|
[HttpPost]
|
||
|
public async Task<IActionResult> SendMessageToUser(string user, string message)
|
||
|
{
|
||
|
await _hubContext.Clients.Group(user).ReceiveMessage(user, message);
|
||
|
return Ok();
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 向指定角色发送消息
|
||
|
/// </summary>
|
||
|
/// <param name="role"></param>
|
||
|
/// <param name="message"></param>
|
||
|
/// <returns></returns>
|
||
|
[HttpPost]
|
||
|
public async Task<IActionResult> SendMessageToRole(string role, string message)
|
||
|
{
|
||
|
await _hubContext.Clients.Group(role).ReceiveMessage(role, message);
|
||
|
return Ok();
|
||
|
}
|
||
|
}
|