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 { /// /// 消息客户端 /// [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.SendAsync("ReceiveMessage", msg); return Ok("Send Successful!"); } /// /// 发送指定的消息给指定的客户端 /// /// /// /// /// [HttpPost("SendCustomUserMessage", Name = "SendCustomUserMessage")] public async Task 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).SendSystemToUser(date); return Ok("Send Successful!"); } } }