12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.SignalR;
- using OASystem.API.OAMethodLib.Hub.HubClients;
- using OASystem.API.OAMethodLib.Hub.Hubs;
- using OASystem.API.OAMethodLib.SignalR.Hubs;
- namespace OASystem.API.Controllers
- {
- /// <summary>
- /// 消息客户端
- /// </summary>
- [Route("api/[controller]/[action]")]
- public class ClientHubController : ControllerBase
- {
- private readonly ILogger<ClientHubController> _logger;
- private readonly IHubContext<ChatHub, IChatClient> _hubContext;
- public ClientHubController(ILogger<ClientHubController> logger, IHubContext<ChatHub, IChatClient> hubContext )
- {
- _logger = logger;
- _hubContext = hubContext;
- }
- /// <summary>
- /// 获取所有的用户
- /// </summary>
- /// <returns></returns>
- [HttpGet("GetAllUserIds", Name = "GetAllUserIds")]
- public List<UserModel> GetAllUserIds()
- {
- return UserStore.OnlineUser;
- }
- /// <summary>
- /// 发送消息给客户端
- /// </summary>
- /// <param name="userid"></param>
- /// <param name="date"></param>
- /// <param name="hubContext"></param>
- /// <returns></returns>
- [HttpPost("SendAllUserMessage", Name = "SendAllUserMessage")]
- public async Task<IActionResult> SendAllUserMessage( string msg)
- {
- await _hubContext.Clients.All.SendAll(msg);
- return Ok("Send Successful!");
- }
- /// <summary>
- /// 发送指定的消息给指定的客户端
- /// </summary>
- /// <param name="userid"></param>
- /// <param name="date"></param>
- /// <param name="hubContext"></param>
- /// <returns></returns>
- [HttpPost("SendCustomUserMessage", Name = "SendCustomUserMessage")]
- public async Task<IActionResult> SendCustomUserMessage(int userid,string date )
- {
- string connId = string.Empty;
- UserModel user = UserStore.OnlineUser.Where(it => it.UserId == userid).FirstOrDefault();
- if (user != null)
- {
- connId = user.ConnectionId;
- }
- await _hubContext.Clients.Client(connId).SendCustomUserMessage(date);
- return Ok("Send Successful!");
- }
- }
- }
|