Exemple #1
0
        /// <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);
            }
        }
Exemple #2
0
        public void LeaveRoom(int roomId)
        {
            var myUserId = this.GetMyUserId();

            ChatHubCache.RemoveUserFromRoom(myUserId, roomId);
            this.BroadcastUserList(roomId, new[] { myUserId });
            this.BroadcastRoomList();
        }
Exemple #3
0
        /// <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());
        }
Exemple #4
0
        public void EnterRoom(int roomId)
        {
            var myUserId = this.GetMyUserId();

            ChatHubCache.AddUserToRoom(myUserId, roomId);
            this.BroadcastUserList(roomId, new[] { myUserId });
            this.BroadcastRoomList();
        }
Exemple #5
0
        /// <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");
        }
Exemple #6
0
        /// <summary>
        ///
        /// </summary>
        private void BroadcastRoomList()
        {
            var connectionsToNotify = ChatHubCache.GetAllConnections();

            foreach (var connectionId in connectionsToNotify)
            {
                this.Clients.Client(connectionId).roomListChanged(new ChatRoomListChangedInfo
                {
                    Rooms = this.GetRoomsList()
                });
            }
        }
Exemple #7
0
 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()
     });
 }
Exemple #8
0
        /// <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
                });
            }
        }
Exemple #9
0
        /// <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)
                });
            }
        }
Exemple #10
0
        /// <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());
        }