Esempio n. 1
0
        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();
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
        }