public async Task SendMessageToEveryUser(ChatMessageVM chatMessage) { var currentUser = await _userManager.FindByNameAsync(Context.User.Identity.Name); chatMessage.SetAuthor(currentUser); await Clients.All.SendAsync("ShowMessage", chatMessage); }
public async Task SendMessageToGroup(ChatMessageVM chatMessage) { var currentUser = await _userManager.FindByNameAsync(Context.User.Identity.Name); chatMessage.SetAuthor(currentUser); int groupId = 0; if (Int32.TryParse(chatMessage.RecipientId, out groupId)) { await Clients.Group(groupId.ToString()).SendAsync("ShowMessage", chatMessage); } }
public ActionResultVM UserMessageReceipt([FromBody] ChatMessageVM cm) { var vm = new ActionResultVM(); try { vm = ccs.HandleUserMessageReceipt(cm, HttpContext, db); } catch (Exception ex) { vm.Set(ex); } return(vm); }
public ActionResultVM PushMessageToUsers([FromBody] ChatMessageVM cm) { var vm = new ActionResultVM(); try { vm = ccs.HandleMessageToUsers(cm, HttpContext, Clients); } catch (Exception ex) { vm.Set(ex); } return(vm); }
/// <summary> /// 处理用户消息回执 /// </summary> /// <param name="cm">发送消息</param> /// <param name="hc">上下文</param> /// <param name="db">数据库</param> /// <returns></returns> public static ActionResultVM HandleUserMessageReceipt(ChatMessageVM cm, HttpContext hc, Data.ContextBase db) { var vm = new ActionResultVM(); if (cm == null) { vm.Set(ARTag.lack); vm.Msg = "消息主体不能为空"; } else if (string.IsNullOrWhiteSpace(cm.CmId)) { vm.Set(ARTag.lack); vm.Msg = "消息ID不能为空"; } else { vm.Set(ARTag.invalid); vm.Msg = "消息ID无效"; //用户连接信息 var cu = GetChatUserInfo(hc); //缓存消息 if (UserMessage1.ContainsKey(cu.UserId)) { var mb = UserMessage1[cu.UserId].FirstOrDefault(x => x.CmuId == cm.CmId); if (mb != null) { mb.CmuStatus = cm.CmStatus; vm.Set(ARTag.success); } } else { var mb = db.NChatMessageToUser.Find(cm.CmId); if (mb != null) { mb.CmuStatus = cm.CmStatus; db.SaveChanges(); vm.Set(ARTag.success); } } } return(vm); }
public async Task SendMessageToSingleUser(ChatMessageVM chatMessage) { var currentUser = await _userManager.FindByNameAsync(Context.User.Identity.Name); chatMessage.SetAuthor(currentUser); int recipientId = 0; if (Int32.TryParse(chatMessage.RecipientId, out recipientId)) { var recipient = _dbContext.Users.FirstOrDefault(u => u.Id == recipientId); if (recipient != null) { await Clients.User(recipient.Id.ToString()).SendAsync("ShowMessage", chatMessage); } } }
/// <summary> /// 推送消息转存储消息 /// </summary> /// <param name="cm"></param> public static List <Domain.NChatMessageToGroup> SaveAsGroupMessage(ChatMessageVM cm) { var listCm = new List <Domain.NChatMessageToGroup>(); cm.CmToIds.ForEach(id => { listCm.Add(new Domain.NChatMessageToGroup() { CmgId = cm.CmId, CmgPushUserId = cm.CmFromId, CmgPullGroupId = id, CmgContent = cm.CmContent.ToJson(), CmgPushWhich = cm.CmWhich, CmgPushType = cm.CmType, CmgCreateTime = cm.CmTime, CmgPushUserDevice = cm.CmFromConn?.UserDevice, CmgPushUserSign = cm.CmFromConn?.ConnSign }); }); return(listCm); }
/// <summary> /// 推送消息转存储消息 /// </summary> /// <param name="cm"></param> public static List <Domain.NChatMessageToUser> WriteMessageForUser(ChatMessageVM cm) { var listCm = new List <Domain.NChatMessageToUser>(); cm.CmToIds.ForEach(id => { listCm.Add(new Domain.NChatMessageToUser() { CmuId = cm.CmId, CmuPushUserId = cm.CmFromId, CmuPullUserId = id, CmuContent = cm.CmContent.ToJson(), CmuPushWhich = cm.CmWhich, CmuPushType = cm.CmType, CmuCreateTime = cm.CmTime, CmuPushUserDevice = cm.CmFromConn?.UserDevice, CmuPushUserSign = cm.CmFromConn?.ConnSign }); }); return(listCm); }
/// <summary> /// 处理用户消息发送 /// </summary> /// <param name="cm">发送消息</param> /// <param name="hc">上下文</param> /// <param name="Clients">连接客户端对象</param> public static ActionResultVM HandleMessageToUsers(ChatMessageVM cm, HttpContext hc, IHubClients Clients) { var vm = new ActionResultVM(); var pmax = GlobalTo.GetValue <int>("NetnrChat:BatchPushUserMax"); var rme = GlobalTo.GetValue("NetnrChat:ReceiveMessage"); if (cm == null) { vm.Set(ARTag.lack); vm.Msg = "消息主体不能为空"; } else if (string.IsNullOrWhiteSpace(cm.CmFromId)) { vm.Set(ARTag.lack); vm.Msg = "发送用户ID不能为空"; } else if (cm.CmContent == null) { vm.Set(ARTag.lack); vm.Msg = "发送内容不能为空"; } else if (!Enum.TryParse(cm.CmType, true, out MessageType mt)) { vm.Set(ARTag.lack); vm.Msg = "消息类型有误"; } else if (cm.CmToIds == null || cm.CmToIds.Count == 0) { vm.Set(ARTag.lack); vm.Msg = "接收用户ID不能为空"; } else if (cm.CmToIds.Count > pmax) { vm.Set(ARTag.refuse); vm.Msg = $"接收用户限制为最多{pmax}"; } else { //接收用户 var users = FindUsers(cm.CmToIds); if (users.Count > 0) { cm.CmId = NewMessageId(); cm.CmType = mt.ToString(); cm.CmTime = DateTime.Now; cm.CmWhich = "User"; //用户连接信息 var cu = GetChatUserInfo(hc); cm.CmFromConn = cu?.Conns.Values.FirstOrDefault(); //开始推送 foreach (var user in users) { var isonline = user.Conns.Count > 0; //在线 if (isonline) { Clients.User(user.UserId).SendAsync(rme, cm); } //写入消息 var cs = isonline ? 2 : 1; var wcm = SaveAsUserMessage(cm); wcm.ForEach(x => x.CmuStatus = cs); AddMessage(wcm); } //发送成功,返回消息ID vm.Data = cm.CmId; vm.Set(ARTag.success); } else { vm.Set(ARTag.invalid); vm.Msg = "接收用户ID无效"; } } return(vm); }
/// <summary> /// 处理组消息发送 /// </summary> /// <param name="cm">发送消息</param> /// <param name="hc">上下文</param> /// <param name="Clients">连接客户端对象</param> public static ActionResultVM HandleMessageToGroup(ChatMessageVM cm, HttpContext hc, IHubClients Clients) { var vm = new ActionResultVM(); var pmax = GlobalTo.GetValue <int>("NetnrChat:BatchPushGroupMax"); var rme = GlobalTo.GetValue("NetnrChat:ReceiveMessage"); if (cm == null) { vm.Set(ARTag.lack); vm.Msg = "消息主体不能为空"; } else if (string.IsNullOrWhiteSpace(cm.CmFromId)) { vm.Set(ARTag.lack); vm.Msg = "发送用户ID不能为空"; } else if (cm.CmContent == null) { vm.Set(ARTag.lack); vm.Msg = "发送内容不能为空"; } else if (!Enum.TryParse(cm.CmType, true, out MessageType mt)) { vm.Set(ARTag.lack); vm.Msg = "消息类型有误"; } else if (cm.CmToIds == null || cm.CmToIds.Count == 0) { vm.Set(ARTag.lack); vm.Msg = "接收组ID不能为空"; } else if (cm.CmToIds.Count > pmax) { vm.Set(ARTag.refuse); vm.Msg = $"接收组限制为最多{pmax}"; } else { //接收用户 var groups = FindGroup(cm.CmToIds); if (groups.Count > 0) { cm.CmId = NewMessageId(); cm.CmType = mt.ToString(); cm.CmTime = DateTime.Now; cm.CmWhich = "Group"; //用户连接信息 var cu = GetChatUserInfo(hc); cm.CmFromConn = cu?.Conns.Values.FirstOrDefault(); //发送消息 var groupids = groups.Select(x => x.GroupId).ToList(); Clients.Groups(groupids).SendAsync(rme, cm); //写入消息 var wcm = SaveAsGroupMessage(cm); AddMessage(wcm); //发送成功,返回消息ID vm.Data = cm.CmId; vm.Set(ARTag.success); } else { vm.Set(ARTag.invalid); vm.Msg = "接收组ID无效"; } } return(vm); }