/// <summary> /// Sends a message to a particular user /// </summary> public void SendMessage(int?roomId, int?conversationId, int?userToId, string message, string clientGuid) { var myUserId = this.GetMyUserId(); var dbChatMessage = new ChatMessage { DateTime = DateTime.UtcNow, Message = message, UserFromId = myUserId, RoomId = roomId, ConversationId = conversationId, UserToId = userToId }; this.Db.ChatMessages.Add(dbChatMessage); this.Db.SaveChanges(); // gets the connections that have to receive the message var connectionIds = ChatHubCache.GetConnectionsToTarget(myUserId, roomId, conversationId, userToId); var chatMessage = this.GetChatMessageInfo(dbChatMessage, clientGuid); foreach (var connectionId in connectionIds) { this.Clients.Client(connectionId).sendMessage(chatMessage); } }
public void LeaveRoom(int roomId) { var myUserId = this.GetMyUserId(); ChatHubCache.RemoveUserFromRoom(myUserId, roomId); this.BroadcastUserList(roomId, new[] { myUserId }); this.BroadcastRoomList(); }
/// <summary> /// Triggered when the user opens a new browser window /// </summary> /// <returns></returns> public override Task OnConnected() { var myUserId = this.GetMyUserId(); ChatHubCache.AddUserConnection(myUserId, this.Context.ConnectionId); return(base.OnConnected()); }
public void EnterRoom(int roomId) { var myUserId = this.GetMyUserId(); ChatHubCache.AddUserToRoom(myUserId, roomId); this.BroadcastUserList(roomId, new[] { myUserId }); this.BroadcastRoomList(); }
/// <summary> /// Returns the user list in the given room or conversation /// </summary> /// <param name="roomId"></param> /// <param name="conversationId"></param> public List <ChatUserInfo> GetUserList(int?roomId, int?conversationId) { if (roomId.HasValue) { var usersInRoom = ChatHubCache.GetRoomUsers(roomId.Value); var dbUsers = this.Db.Users.Where(u => usersInRoom.Contains(u.Id)).ToList(); return(dbUsers.Select(u => this.GetUserInfo(u, ChatUserInfo.StatusType.Online)).ToList()); } throw new NotImplementedException("Conversations are not supported yet"); }
/// <summary> /// /// </summary> private void BroadcastRoomList() { var connectionsToNotify = ChatHubCache.GetAllConnections(); foreach (var connectionId in connectionsToNotify) { this.Clients.Client(connectionId).roomListChanged(new ChatRoomListChangedInfo { Rooms = this.GetRoomsList() }); } }
private ChatRoomInfo GetChatRoomInfo(ChatRoom chatRoom) { if (chatRoom == null) { throw new ArgumentNullException("chatRoom"); } return(new ChatRoomInfo { Id = chatRoom.Id, Name = chatRoom.Name, UsersOnline = ChatHubCache.GetRoomUsers(chatRoom.Id).Count() }); }
/// <summary> /// Broadcasts to all users in the same room the new users list /// </summary> private void BroadcastUserList(int roomId, int[] exceptUsers = null) { var connectionsToNotify = ChatHubCache.GetRoomConnections(roomId, exceptUsers); var usersInRoom = this.GetUserList(roomId, null); foreach (var connectionId in connectionsToNotify) { this.Clients.Client(connectionId).userListChanged(new ChatUserListChangedInfo { RoomId = roomId, ConversationId = null, UserList = usersInRoom }); } }
/// <summary> /// Sends a typing signal to a particular user /// </summary> public void SendTypingSignal(int?roomId, int?conversationId, int?userToId) { var myUserId = this.GetMyUserId(); var connectionIds = ChatHubCache.GetConnectionsToTarget(myUserId, roomId, conversationId, userToId); foreach (var connectionId in connectionIds) { this.Clients.Client(connectionId).sendTypingSignal(new ChatTypingSignalInfo { RoomId = roomId, ConversationId = conversationId, UserToId = userToId, UserFrom = this.GetUserInfo(myUserId) }); } }
/// <summary> /// Triggered when the user closes the browser window /// </summary> /// <returns></returns> public override Task OnDisconnected() { //ChatHubCache.RemoveUserConnection(this.Context.ConnectionId); var disconnectedUserId = ChatHubCache.GetUserIdFromConnection(this.Context.ConnectionId); ChatHubCache.RemoveUserConnection(this.Context.ConnectionId); var roomsId = new int[0]; if (disconnectedUserId.HasValue) { if (!ChatHubCache.GetUsersConnections(new[] { disconnectedUserId.Value }).Any()) { // in this case, the user DID NOT connect back after the disconnect. // This user now should be removed from all rooms and conversations he/she is in roomsId = ChatHubCache.RoomsFromUser(disconnectedUserId.Value); // drops the user from the cache ChatHubCache.DropUser(disconnectedUserId.Value); } if (roomsId.Any()) { Task.Run(() => { Thread.Sleep(2000); if (!ChatHubCache.GetUsersConnections(new[] { disconnectedUserId.Value }).Any()) { foreach (var roomId in roomsId) { this.BroadcastUserList(roomId); } this.BroadcastRoomList(); } }); } } return(base.OnDisconnected()); }