Ejemplo n.º 1
0
 public bool EndChat(ChatMessage chatMessage)
 {
     try
     {
         ChatRoom chatRoom;
         if (_chatRooms.TryGetValue(chatMessage.conversationId, out chatRoom))
         {
             if (_chatRooms[chatRoom.chatRoomId].chatRoomInitiatedBy == chatMessage.senderId)
             {
                 chatMessage.messageText = string.Format("{0} left the chat. Chat Ended!", chatMessage.senderName);
                 if (_chatRooms.TryRemove(chatRoom.chatRoomId, out chatRoom))
                 {
                     Clients[chatRoom.chatRoomId].receiveEndChatMessage(chatMessage);
                     foreach (MessageRecipient messageReceipient in chatRoom.messageRecipients)
                     {
                         if (messageReceipient.chatRoomIds.Contains(chatRoom.chatRoomId))
                         {
                             messageReceipient.chatRoomIds.Remove(chatRoom.chatRoomId);
                             Groups.Remove(messageReceipient.connectionId, chatRoom.chatRoomId);
                         }
                     }
                 }
             }
             else
             {
                 MessageRecipient messageRecipient = GetChatUserByUserId(chatMessage.senderId);
                 if (messageRecipient != null && messageRecipient.chatRoomIds.Contains(chatRoom.chatRoomId))
                 {
                     chatRoom.messageRecipients.Remove(messageRecipient);
                     messageRecipient.chatRoomIds.Remove(chatRoom.chatRoomId);
                     if (chatRoom.messageRecipients.Count < 2)
                     {
                         chatMessage.messageText = string.Format("{0} left the chat. Chat Ended!", chatMessage.senderName);
                         if (_chatRooms.TryRemove(chatRoom.chatRoomId, out chatRoom))
                         {
                             Clients[chatRoom.chatRoomId].receiveEndChatMessage(chatMessage);
                             foreach (MessageRecipient messageReceipient in chatRoom.messageRecipients)
                             {
                                 if (messageReceipient.chatRoomIds.Contains(chatRoom.chatRoomId))
                                 {
                                     messageReceipient.chatRoomIds.Remove(chatRoom.chatRoomId);
                                     Groups.Remove(messageReceipient.connectionId, chatRoom.chatRoomId);
                                 }
                             }
                         }
                     }
                     else
                     {
                         chatMessage.messageText = string.Format("{0} left the chat.", chatMessage.senderName);
                         Groups.Remove(messageRecipient.connectionId, chatRoom.chatRoomId);
                         Clients[messageRecipient.connectionId].receiveEndChatMessage(chatMessage);
                         Clients[chatRoom.chatRoomId].receiveLeftChatMessage(chatMessage);
                         Clients[chatRoom.chatRoomId].updateChatUI(chatRoom);
                     }
                 }
             }
         }
         else
         {
             throw new InvalidOperationException("Problem in ending chat!");
         }
         return true;
     }
     catch (Exception ex)
     {
         throw new InvalidOperationException("Problem in ending chat!");
     }
 }
Ejemplo n.º 2
0
 public bool SendChatMessage(ChatMessage chatMessage)
 {
     try
     {
         ChatRoom chatRoom;
         if (_chatRooms.TryGetValue(chatMessage.conversationId, out chatRoom))
         {
             chatMessage.chatMessageId = Guid.NewGuid().ToString();
             chatMessage.timestamp = DateTime.Now;
             Clients[chatMessage.conversationId].receiveChatMessage(chatMessage, chatRoom);
             return true;
         }
         else
         {
             throw new InvalidOperationException("Problem in sending message!");
         }
     }
     catch (Exception ex)
     {
         throw new InvalidOperationException("Problem in sending message!");
     }
 }
Ejemplo n.º 3
0
        private Boolean DeleteUser(string connectionId)
        {
            var returnValue = false;
            var user = GetChatUserByConnectionId(connectionId);
            if (user != null && _chatUsers.ContainsKey(user.messageRecipientId))
            {
                MessageRecipient messageRecipient;
                returnValue = _chatUsers.TryRemove(user.messageRecipientId, out messageRecipient);

                //remoave from all groups and chatrooms
                foreach (string chatRoomId in messageRecipient.chatRoomIds)
                {
                    _chatRooms[chatRoomId].messageRecipients.Remove(messageRecipient);

                    Groups.Remove(messageRecipient.connectionId, chatRoomId);

                    //notify user left chat
                    ChatMessage chatMessage = new ChatMessage();
                    chatMessage.conversationId = chatRoomId;
                    chatMessage.senderId = messageRecipient.messageRecipientId;
                    chatMessage.senderName = messageRecipient.messageRecipientName;
                    if (_chatRooms[chatRoomId].chatRoomInitiatedBy == messageRecipient.messageRecipientId)
                    {
                        chatMessage.messageText = string.Format("{0} left the chat. Chat Ended!", messageRecipient.messageRecipientName);
                        ChatRoom chatRoom;

                        if (_chatRooms.TryRemove(chatRoomId, out chatRoom))
                        {
                            foreach (MessageRecipient messageReceipient in chatRoom.messageRecipients)
                            {
                                if (messageReceipient.chatRoomIds.Contains(chatRoomId))
                                {
                                    messageReceipient.chatRoomIds.Remove(chatRoomId);
                                }
                            }
                            Clients[chatRoomId].receiveEndChatMessage(chatMessage);
                        }
                    }
                    else
                    {
                        if (_chatRooms[chatRoomId].messageRecipients.Count() < 2)
                        {
                            chatMessage.messageText = string.Format("{0} left the chat. Chat Ended!", messageRecipient.messageRecipientName);
                            ChatRoom chatRoom;
                            if (_chatRooms.TryRemove(chatRoomId, out chatRoom))
                            {
                                foreach (MessageRecipient messageReceipient in chatRoom.messageRecipients)
                                {
                                    if (messageReceipient.chatRoomIds.Contains(chatRoomId))
                                    {
                                        messageReceipient.chatRoomIds.Remove(chatRoomId);
                                    }
                                }
                                Clients[chatRoomId].receiveEndChatMessage(chatMessage);
                            }
                        }
                        else
                        {
                            chatMessage.messageText = string.Format("{0} left the chat.", messageRecipient.messageRecipientName);
                            Clients[chatRoomId].receiveLeftChatMessage(chatMessage);
                        }
                    }
                }
            }
            return returnValue;
        }
Ejemplo n.º 4
0
        public bool InitiateChat(string fromUserId, string fromUserName, string toUserId, string toUserName)
        {
            try
            {
                if (string.IsNullOrEmpty(fromUserId) || string.IsNullOrEmpty(fromUserName) || string.IsNullOrEmpty(toUserId) || string.IsNullOrEmpty(toUserName))
                {
                    return false;
                }

                var fromUser = GetChatUserByUserId(fromUserId);
                var toUser = GetChatUserByUserId(toUserId);

                if (fromUser != null && toUser != null)
                {
                    if (!CheckIfRoomExists(fromUser, toUser))
                    {
                        //Create New Chat Room
                        ChatRoom chatRoom = new ChatRoom();
                        chatRoom.chatRoomInitiatedBy = fromUser.messageRecipientId;
                        chatRoom.chatRoomInitiatedTo = toUser.messageRecipientId;

                        chatRoom.messageRecipients.Add(fromUser);
                        chatRoom.messageRecipients.Add(toUser);

                        //create and save blank message to get new conversation id
                        ChatMessage chatMessage = new ChatMessage();
                        chatMessage.messageText = "Chat Initiated";
                        chatMessage.senderId = fromUser.messageRecipientId;
                        chatMessage.senderName = fromUser.messageRecipientName;

                        fromUser.chatRoomIds.Add(chatRoom.chatRoomId);
                        toUser.chatRoomIds.Add(chatRoom.chatRoomId);

                        //Create SignalR Group for this chat room and add users connection to it
                        Groups.Add(fromUser.connectionId, chatRoom.chatRoomId);
                        Groups.Add(toUser.connectionId, chatRoom.chatRoomId);

                        //Add Chat room object to collection
                        if (_chatRooms.TryAdd(chatRoom.chatRoomId, chatRoom))
                        {
                            //Generate Client UI for this room
                            Clients[fromUser.connectionId].initiateChatUI(chatRoom);
                        }
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Problem in starting chat!");
            }
        }