using Microsoft.AspNetCore.SignalR; using System; using System.Collections.Generic; using System.Threading.Tasks; using HT.Cloud.Code; using HT.Cloud.Domain.InfoManage; using HT.Cloud.Service.InfoManage; using HT.Cloud.Service.SystemOrganize; namespace HT.Cloud.Service { public class MessageHub : Hub { private string cacheKey = GlobalContext.SystemConfig.ProjectPrefix + "_hubuserinfo_"; private readonly UserService _service; private readonly MessageService _msgService; private string cacheKeyOperator = GlobalContext.SystemConfig.ProjectPrefix + "_operator_";// +登录者tokens public MessageHub(UserService service, MessageService msgService) { _service = service; _msgService = msgService; } // /// 客户端登录到服务器 /// /// public async Task SendLogin(string token) { var user = _service.currentuser; if (user == null || user.UserId == null) { user = CacheHelper.Get(cacheKeyOperator + token); } if (user != null && user.CompanyId != null) { //一个公司一个分组 await Groups.AddToGroupAsync(Context.ConnectionId, user.CompanyId); //将用户信息存进缓存 var list = await CacheHelper.GetAsync>(cacheKey + user.UserId) ?? new List(); //登录计数 var onlinelist = await CacheHelper.GetAsync>(cacheKey + "list_" + user.CompanyId) ?? new List(); list.Add(Context.ConnectionId); onlinelist.Add(Context.ConnectionId); await CacheHelper.SetAsync(cacheKey + Context.ConnectionId, user.UserId); await CacheHelper.SetAsync(cacheKey + user.UserId, list); await CacheHelper.SetAsync(cacheKey + "list_" + user.CompanyId, onlinelist); } } /// /// 发送消息 /// /// 收消息的人员Id /// 消息内容 /// public async Task SendMessage(string reUserId, string message) { if (string.IsNullOrEmpty(reUserId)) { return; } else { MessageEntity msg = new MessageEntity(); msg.F_EnabledMark = true; msg.F_MessageType = 1; msg.F_CreatorUserName = _service.currentuser.UserName; msg.F_MessageInfo = message; msg.F_ToUserId = reUserId; msg.F_ClickRead = true; await _msgService.SubmitForm(msg); } } public override async Task OnDisconnectedAsync(Exception exception) { var user = _service.currentuser; //删除缓存连接 var userId = await CacheHelper.GetAsync(cacheKey + Context.ConnectionId); if (!string.IsNullOrEmpty(userId)) { //将用户信息存进缓存 var list = await CacheHelper.GetAsync>(cacheKey + userId); //登录计数 var onlinelist = await CacheHelper.GetAsync>(cacheKey + "list_" + user.CompanyId); if (list != null) { list.Remove(Context.ConnectionId); if (list.Count == 0) { await CacheHelper.RemoveAsync(cacheKey + userId); } else { await CacheHelper.SetAsync(cacheKey + userId, list); } } if (onlinelist != null) { onlinelist.Remove(Context.ConnectionId); if (list.Count == 0) { await CacheHelper.RemoveAsync(cacheKey + "list_" + user.CompanyId); } else { await CacheHelper.SetAsync(cacheKey + "list_" + user.CompanyId, onlinelist); } } await CacheHelper.RemoveAsync(cacheKey + Context.ConnectionId); } await base.OnDisconnectedAsync(exception); } } }