using Microsoft.AspNetCore.SignalR;
using OASystem.API.OAMethodLib;
using OASystem.API.OAMethodLib.Hub.HubClients;
using OASystem.API.OAMethodLib.Hub.Hubs;
using OASystem.API.OAMethodLib.SignalR.Hubs;
namespace OASystem.API.Controllers
{
///
/// 消息客户端
///
[Route("api/[controller]/[action]")]
public class ClientHubController : ControllerBase
{
private readonly ILogger _logger;
private readonly IHubContext _hubContext;
public ClientHubController(ILogger logger, IHubContext hubContext)
{
_logger = logger;
_hubContext = hubContext;
}
///
/// 获取在线用户
///
///
[HttpGet("GetOnlineUsers", Name = "GetOnlineUsers")]
public List GetOnlineUsers()
{
return UserStore.OnlineUser;
}
///
/// 发送消息给客户端
///
///
///
///
///
[HttpPost("SendAllUserMessage", Name = "SendAllUserMessage")]
public async Task SendAllUserMessage(string msg)
{
await _hubContext.Clients.All.ReceiveMessage(msg);
return Ok("Send Successful!");
}
///
/// 发送指定的消息给指定的客户端
///
///
///
///
///
[HttpPost("SendCustomUserMessage", Name = "SendCustomUserMessage")]
public async Task SendCustomUserMessage(List userIds, string title, string content)
{
string connId = string.Empty;
//UserModel user = UserStore.OnlineUser.Where(it => it.UserId == userId).FirstOrDefault();
//if (user != null)
//{
// connId = user.ConnectionId;
//}
//else
//{
// return Ok("Send Failed! User Not Online!");
//}
//await _hubContext.Clients.Client(connId).ReceiveMessage(date);
GeneralMethod.MessageIssueAndNotification(MessageTypeEnum.Announcement, title, content, userIds);
return Ok("Send Successful!");
}
///
/// 消息 发布And 通知
///
///
///
///
///
///
[HttpPost("MessageIssuserAndNotification", Name = "MessageIssuserAndNotification")]
public async Task MessageIssuserAndNotification(MessageTypeEnum msgTypeEnum, string title, string content, List userIds)
{
var status = await GeneralMethod.MessageIssueAndNotification(msgTypeEnum, title, content, userIds);
if (status)
{
return Ok("Operation Successful!");
}
else
{
return Ok("Operation Fail!");
}
}
}
}