Exemplo n.º 1
0
        /// <summary>
        /// When the method is called with no arguments, just return the view
        /// When argument logOn is true, a user logged on
        /// When argument logOff is true, a user closed their browser or navigated away (log off)
        /// When argument chatMessage is specified, the user typed something in the chat
        /// </summary>
        public ActionResult Index(bool? logOn, bool? logOff, string chatMessage)
        {
            string UserID = User.Identity.GetUserId();

            if (UserID != null)
            {
            try
            {
                if (chatModel == null) chatModel = new ChatModel();
                ChatModel.ChatUser currentUser = chatModel.Users.FirstOrDefault(u => u.ChatUserID == User.Identity.GetUserId());
                if (logOn != null && (bool)logOn)
                {
                    return CheckLoggedInUser(UserID, chatModel);
                }

                else if (logOff != null && (bool)logOff)
                {
                    LogOffUser(chatModel.Users.FirstOrDefault(u => u.ChatUserID == User.Identity.GetUserId()));
                    ChatViewModel chatViewModel = new ChatViewModel();
                    chatViewModel.Users = chatModel.Users;
                    chatViewModel.ChatHistory = currentUser.ChatHistory;
                    return PartialView("Lobby", chatViewModel);
                }
                else
                {

                    //remember each user's last ping time
                    currentUser.LastPing = DateTime.Now;

                    #region remove inactive users
                    RemoveInactiveUsers(chatModel);
                    #endregion

                    #region if there is a new message, append it to the chat
                    if (!string.IsNullOrEmpty(chatMessage))
                    {
                        AddMessage(UserID, chatMessage, chatModel);
                    }
                    #endregion
                    ChatViewModel chatViewModel = new ChatViewModel();
                    chatViewModel.Users = chatModel.Users;
                    chatViewModel.ChatHistory = currentUser.ChatHistory;
                    return PartialView("ChatHistory", chatViewModel);
                }
            }
            catch (Exception ex)
            {
                //return error to AJAX function
                Response.StatusCode = 500;
                return Content(ex.Message);
            }
            }
            else
            {
            return View();
            }
        }
Exemplo n.º 2
0
        private ActionResult CheckLoggedInUser(string UserID, ChatModel chatModel)
        {
            if (chatModel.Users.FirstOrDefault(u => u.ChatUserID == User.Identity.GetUserId()) == null)
            {
            #region create new user and add to lobby

                ChatModel.ChatUser currentUser = new ChatModel.ChatUser()
            {
                ChatUserID = UserID,
                Name = User.Identity.GetUserName(),
                LoggedOnTime = DateTime.Now,
                LastPing = DateTime.Now
            };
                chatModel.Users.Add(currentUser);

            //stored procedure select
            MySqlConnection selectConn = new MySqlConnection();
            selectConn.CreateConn();
            SqlCommand command = new SqlCommand("GetChatMessages", selectConn.Connection);
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@UserID", UserID));

            selectConn.DataReader = command.ExecuteReader();
            while (selectConn.DataReader.Read())
            {
                currentUser.ChatHistory.Add(new ChatModel.ChatMessage
                {
                    Message = selectConn.DataReader["MessageContent"].ToString(),
                    Username = selectConn.DataReader["UserName"].ToString()
                });
            }
            #endregion
            }
            ChatModel.ChatUser curUser = chatModel.Users.FirstOrDefault(u => u.ChatUserID == User.Identity.GetUserId());
            ChatViewModel chatViewModel = new ChatViewModel();
            chatViewModel.Users = chatModel.Users;
            chatViewModel.ChatHistory = curUser.ChatHistory;
            return PartialView("Lobby", chatViewModel);
        }