public override Task OnDisconnected() { if (Context.User != null && Context.User.Identity.IsAuthenticated) { using (var chatContext = new ChatContext()) { User user = chatContext.Users.FirstOrDefault(x => x.Name == Context.User.Identity.Name); if (user != null) { user.LastActivity = DateTime.Now; var connection = user.HubConnections.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId); if (connection != null) chatContext.Entry(connection).State = EntityState.Deleted; chatContext.Entry(user).State = EntityState.Modified; chatContext.SaveChanges(); ChatUser chatUser = new ChatUser() { Id = user.Id, Name = WebUtility.HtmlEncode(user.Name) }; string[] userExistingConnections = user.HubConnections.Select(x => x.ConnectionId).ToArray(); Clients.AllExcept(userExistingConnections).userDisconnected(chatUser); } } } return base.OnDisconnected(); }
public ActionResult CheckIfLogged() { AjaxTypedResult<ChatUser> result = new AjaxTypedResult<ChatUser>(); if (User.Identity.IsAuthenticated) { using (var context = new ChatContext()) { User user = context.Users.FirstOrDefault(x => x.Name == User.Identity.Name); if (user != null) { result.Model = new ChatUser() { Name = user.Name, Id = user.Id }; result.Success = true; user.LastActivity = DateTime.Now; context.Entry(user).State = EntityState.Modified; context.SaveChanges(); } } } return this.JsonCamelCase(result, JsonRequestBehavior.AllowGet); }
public ActionResult Test() { Response.Write("HomeController Test start..."); using (ChatContext context = new ChatContext()) { User user = new User(); user.Name = "Piotrek - " + Guid.NewGuid().ToString(); user.LastActivity = DateTime.Now; user.HubConnections.Add(new HubConnection() { ConnectionId = Guid.NewGuid().ToString().Substring(2, 10) }); user.HubConnections.Add(new HubConnection() { ConnectionId = Guid.NewGuid().ToString().Substring(2, 10) }); context.Entry(user).State = EntityState.Added; context.SaveChanges(); } using (ChatContext context = new ChatContext()) { foreach (var user in context.Users) { Response.Write("<br />" + user.Name); if (user.HubConnections.Count() > 0) { Response.Write("<br /> User's hub connections (" + user.HubConnections.Count() + ")"); foreach (var conn in user.HubConnections) { Response.Write("<br /> - " + conn.ConnectionId); } } } } return new EmptyResult(); }
public ActionResult Index() { using (var context = new ChatContext()) { var inactiveUsers = context.Users.Where(x => x.LastActivity < EntityFunctions.AddMinutes(DateTime.Now, -60)); foreach (var user in inactiveUsers) context.Entry(user).State = EntityState.Deleted; context.SaveChanges(); } return View(); }
public ActionResult Create(ChatUser model) { AjaxTypedResult<ChatUser> result = new AjaxTypedResult<ChatUser>(); if (!User.Identity.IsAuthenticated && ModelState.IsValid) { using (var context = new ChatContext()) { User user = context.Users.FirstOrDefault(x => x.Name == model.Name); if (user == null) { User newUser = new User() { Name = model.Name, LastActivity = DateTime.Now }; context.Entry(newUser).State = EntityState.Added; context.SaveChanges(); result.Model = new ChatUser() { Name = newUser.Name, Id = newUser.Id }; result.Success = true; result.SuccessMessage = "Użytkownik został dodany."; FormsAuthentication.SetAuthCookie(model.Name, true); } else result.Errors.Add("Wystąpił błąd. Nazwa użytkownika jest zajęta."); } } else result.Errors.Add("Wystąpił błąd. Użytkownik jest już zalogowany lub nazwa jest niepoprawna."); return this.JsonCamelCase(result); }
public ActionResult Delete() { AjaxResult result = new AjaxResult(); if (User.Identity.IsAuthenticated) { using (var context = new ChatContext()) { User user = context.Users.FirstOrDefault(x => x.Name == User.Identity.Name); if (user != null) { context.Entry(user).State = EntityState.Deleted; context.SaveChanges(); result.Success = true; FormsAuthentication.SignOut(); } } } return this.JsonCamelCase(result, JsonRequestBehavior.AllowGet); }
public void SendMessage(string message) { ChatUser chatUser = null; if (Context.User != null && Context.User.Identity.IsAuthenticated) { using (var chatContext = new ChatContext()) { User user = chatContext.Users.FirstOrDefault(x => x.Name == Context.User.Identity.Name); if (user != null) { user.LastActivity = DateTime.Now; chatContext.Entry(user).State = EntityState.Modified; chatContext.SaveChanges(); chatUser = new ChatUser() { Id = user.Id, Name = user.Name }; } } } message = WebUtility.HtmlEncode(message); message = message.Replace("\n", "<br />"); message = message.Replace("\r\n", "<br />"); ExpandUrlsParser urlsParser = new ExpandUrlsParser(); urlsParser.Target = "_blank"; message = urlsParser.ExpandUrls(message); EmoticonParser emoticonParser = new EmoticonParser(); message = emoticonParser.Parse(message); if (chatUser != null) Clients.All.appendMessage(chatUser, message); }