// Sending the userId from the request body as this is just a demo. // On your application you probably want to fetch this from your authentication context and not receive it as a parameter public IActionResult ListFriends([FromBody] dynamic payload) { return(Json(GroupChatHub.ConnectedParticipants((string)payload.currentUserId))); // Use the following for group chats // Make sure you have [pollFriendsList] set to true for this simple group chat example to work as // broadcasting with group was not implemented here // return Json(GroupChatHub.ConnectedParticipants((string)payload.currentUserId)); }
protected override BaseResponseResult DoWork(JoinRoomReq param) { BaseResponseResult rc = new BaseResponseResult((int)Code.OperationError, "操作失败!"); if (Index.User != null) { if (param.liveId > 0) { //找到当前直播对应的房间号 LiveChatRoom room = new LiveChatRoomBLL().Find(it => it.LiveID == param.liveId && it.Status == 1); //如果房间存在 if (room != null && room.ID > 0) { //将ConnectionID与UserID绑定到当前直播的房间中 LiveChatRoomMember member = new LiveChatRoomMember { ConnectionID = param.connectionId, RoomID = room.ID, UserID = Index.User.UserID }; member.ID = new LiveChatRoomMemberBLL().Add(member); //如果当前登陆的人员 成功 加入到直播聊天室 if (member.ID > 0) { //这里的代码很重要,这是在外部调用GroupChatHub var context = GlobalHost.ConnectionManager.GetHubContext <GroupChatHub>(); //将当前的ConnectionID加入到 以房间ID为名称的组中 context.Groups.Add(param.connectionId, room.ID.ToString()); //向客户端发送新加入人员信息 context.Clients.Group(room.ID.ToString()).publishMsg(GroupChatHub.FormatMsg("系统消息", Index.User.UserName + " 加入聊天", 0, Index.User.HeadPic)); rc.SetResult(0, "成功加入聊天室!"); } else { rc.SetResult(3, "加入聊天房间失败!"); } } else { rc.SetResult(1, "当前聊天房间不存在!"); } } else { rc.SetResult(1, "当前聊天房间不存在!"); } } else { rc.SetResult(2, "未登录!"); } return(rc); }
public async Task <IActionResult> UserList([FromBody] dynamic payload) { string srchTxt = payload.searchText; string userId = this.HttpContext.User.GetClaim(OpenIdConnectConstants.Claims.Subject); var connectedPart = GroupChatHub.getConnectedParticpant(userId).FirstOrDefault(); var userList = ( from u in this._ctx.Users where u.Id != userId orderby u.Email select new { DisplayName = u.Email, UserId = u.Id, ParticipantType = ChatParticipantTypeEnum.User, Status = connectedPart == null ? EnumChatParticipantStatus.Offline : EnumChatParticipantStatus.Online, Email = u.Email } ); return(Json(await userList.ToArrayAsync())); }
public async Task <IActionResult> Participants([FromBody] dynamic payload) { string srchTxt = payload.searchText; string userId = this.HttpContext.User.GetClaim(OpenIdConnectConstants.Claims.Subject); string userEmail = this._ctx.Users.Find(userId).Email; var msgListQuery = (from g in this._ctx.ChatGroups.Include(d => d.DateSeen).ThenInclude(d => d.User) join p in this._ctx.Participants.Include(d => d.User) on g equals p.Group into arrPart join ms in this._ctx.ChatMessages on g equals ms.ChatGroup into chatMessages where g.ParticipantType == EnumChatGroupParticipantType.user && arrPart.Any(d => d.User.Id == userId) select new { Particpants = arrPart, ChatGroup = g, UnreadMessageCount = chatMessages .Where(d => d.DateSent > g.DateSeen .Where(s => s.User.Id == userId) .Select(ds => ds.DateSeen) .Max()) .Count() } ); Console.WriteLine(msgListQuery.ToString()); var msgList = await msgListQuery.ToArrayAsync(); var userList = await( from u in this._ctx.Users where !msgList.Any(d => d.Particpants.Any(p => p.User == u)) && u.Id != userId select new { Id = u.Id, Email = u.Email, } ).ToArrayAsync(); List <ParticipantResponseViewModel> resp = new List <ParticipantResponseViewModel>(); foreach (var item in msgList) { var part = new ParticipantResponseViewModel(); part.Participants = new List <ChatParticipantViewModel>(); foreach (var p in item.Particpants.OrderBy(d => d.User.Id == userId)) { var connectedPart = GroupChatHub.getConnectedParticpant(p.User.Id).FirstOrDefault(); part.Participants.Add(new ChatParticipantViewModel() { DisplayName = p.User.Email, UserId = p.User.Id, ParticipantType = ChatParticipantTypeEnum.User, Status = connectedPart == null ? EnumChatParticipantStatus.Offline : EnumChatParticipantStatus.Online, Email = p.User.Email }); } part.Metadata = new ParticipantMetadataViewModel() { TotalUnreadMessages = item.UnreadMessageCount, Title = item.ChatGroup.Title, GroupId = item.ChatGroup != null?item.ChatGroup.Id.ToString() : "" }; resp.Add(part); } foreach (var user in userList) { var part = new ParticipantResponseViewModel(); part.Participants = new List <ChatParticipantViewModel>(); var connectedPart = GroupChatHub.getConnectedParticpant(user.Id).FirstOrDefault(); part.Participants.Add(new ChatParticipantViewModel() { DisplayName = user.Email, UserId = user.Id, ParticipantType = ChatParticipantTypeEnum.User, Status = connectedPart == null ? EnumChatParticipantStatus.Offline : EnumChatParticipantStatus.Online, Email = user.Email }); part.Participants.Add(new ChatParticipantViewModel() { DisplayName = userEmail, UserId = userId, ParticipantType = ChatParticipantTypeEnum.User, Status = EnumChatParticipantStatus.Online, Email = userEmail }); part.Metadata = new ParticipantMetadataViewModel() { TotalUnreadMessages = 0, GroupId = "", Title = part.Participants.First().DisplayName }; resp.Add(part); } return(Json(resp.OrderBy(d => d.Participants.Any(p => p.Status == EnumChatParticipantStatus.Online)).ThenBy(d => d.Metadata.Title))); }