using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.SignalR; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using VOL.Core.CacheManager; using VOL.Core.Extensions; using VOL.Core.ManageUser; using VOL.System.IServices; namespace VOL.WebApi.Controllers.Hubs { /// /// https://docs.microsoft.com/zh-cn/aspnet/core/signalr/introduction?view=aspnetcore-3.1 /// https://docs.microsoft.com/zh-cn/aspnet/core/signalr/javascript-client?view=aspnetcore-6.0&tabs=visual-studio /// public class HomePageMessageHub : Hub { private readonly ICacheService _cacheService; private static ConcurrentDictionary _connectionIds = new ConcurrentDictionary(); /// /// 构造 注入 /// public HomePageMessageHub(ICacheService cacheService) { _cacheService = cacheService; } /// /// 建立连接时异步触发 /// /// public override async Task OnConnectedAsync() { //Console.WriteLine($"建立连接{Context.ConnectionId}"); _connectionIds[Context.ConnectionId] = Context.GetHttpContext().Request.Query["userName"].ToString(); //添加到一个组下 //await Groups.AddToGroupAsync(Context.ConnectionId, "SignalR Users"); //发送上线消息 //await Clients.All.SendAsync("ReceiveHomePageMessage", 1, new { title = "系统消息", content = $"{Context.ConnectionId} 上线" }); await base.OnConnectedAsync(); } /// /// 离开连接时异步触发 /// /// /// public override async Task OnDisconnectedAsync(Exception ex) { //Console.WriteLine($"断开连接{Context.ConnectionId}"); //从组中删除 // await Groups.RemoveFromGroupAsync(Context.ConnectionId, "SignalR Users"); //可自行调用下线业务处理方法... await UserOffline(); //发送下线消息 // await Clients.All.SendAsync("ReceiveHomePageMessage", 4, new { title = "系统消息", content = $"{Context.ConnectionId} 离线" }); await base.OnDisconnectedAsync(ex); } /// /// 根据用户名获取所有的客户端 /// /// /// private IEnumerable GetCnnectionIds(string username) { foreach (var item in _connectionIds) { if (item.Value == username) { yield return item.Key; } } } /// /// 发送给指定的人 /// /// sys_user表的登陆帐号 /// 发送的消息 /// public async Task SendHomeMessage(string username, string title, string message) { if (_connectionIds[Context.ConnectionId]!="admin") { return false; } await Clients.Clients(GetCnnectionIds(username).ToArray()).SendAsync("ReceiveHomePageMessage", new { // username, title, message, date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:sss") }); return true; } /// /// 断开连接 /// /// public async Task UserOffline() { var cid = Context.ConnectionId;//也可以从缓存中获取ConnectionId // await Clients.Client(cid).SendAsync("ReceiveHomePageMessage", 3, new { title = "系统消息", content = "离线成功" }); //移除缓存 if (_connectionIds.TryRemove(cid, out string value)) { } await Task.CompletedTask; return true; } } }