public async Task SendPrivateMessage(string message, string groupName, int fromUserId, int toUser) { if (Clients != null) { PrivateMessageHistory pmh = new PrivateMessageHistory(_context, fromUserId, toUser); var userInfo = pmh.GetMessages(); PrivateMessage privateMessage = new PrivateMessage(); privateMessage.Text = message; privateMessage.UserId = fromUserId; privateMessage.ToUserId = toUser; privateMessage.TimeStamp = DateTime.Now; _context.PrivateMessage.AddRange(privateMessage); _context.SaveChanges(); var name = _context.ModuleUser.Where(u => u.UserId == fromUserId).Select(u => u.FirstName + " " + u.LastName).First(); await Clients.Group(groupName).SendAsync("addMessage", message, groupName, fromUserId, toUser, name, userInfo); // Clients.Group(groupName).addMessage(message, groupName, fromUserId, toUser, name, userInfo); } }
public async Task CreateGroup(int currentUserId, int toConnectTo) { string strGroupName = GetUniqueGroupName(currentUserId, toConnectTo); string connectionId_To = OnLineUser.onLineUserList.Where(item => item.UserId == toConnectTo).Select(item => item.ConnectionId).SingleOrDefault(); string name = OnLineUser.onLineUserList.Where(item => item.UserId == toConnectTo).Select(item => item.User.LastName + " " + item.User.FirstName).First(); if (!string.IsNullOrEmpty(connectionId_To)) { PrivateMessageHistory pmh = new PrivateMessageHistory(_context, currentUserId, toConnectTo); await Groups.AddToGroupAsync(Context.ConnectionId, strGroupName); await Groups.AddToGroupAsync(connectionId_To, strGroupName); await Clients.Caller.SendAsync("setChatWindow", strGroupName, toConnectTo, name, pmh.GetMessages()); //Clients.Caller.setChatWindow(strGroupName, toConnectTo, name, pmh.GetMessages()); } }