Exemple #1
0
        private void AddPrivateMessageinCache(string fromEmail, string chatToEmail, string userName, string message)
        {
            using (mycaryardEntities dc = new mycaryardEntities())
            {
                // Save master
                var master = dc.ChatPrivateMessageMasters.ToList().Where(a => a.EmailID.Equals(fromEmail)).ToList();
                if (master.Count == 0)
                {
                    var result = new ChatPrivateMessageMaster
                    {
                        EmailID  = fromEmail,
                        UserName = userName
                    };
                    dc.ChatPrivateMessageMasters.Add(result);
                    dc.SaveChanges();
                }

                // Save details
                var resultDetails = new ChatPrivateMessageDetail
                {
                    MasterEmailID = fromEmail,
                    ChatToEmailID = chatToEmail,
                    Message       = message
                };
                dc.ChatPrivateMessageDetails.Add(resultDetails);
                dc.SaveChanges();
            }
        }
Exemple #2
0
        public List <PrivateChatMessage> GetScrollingChatData(string fromid, string toid, int start = 10, int length = 1)
        {
            takeCounter = (length * start);       // 20
            skipCounter = ((length - 1) * start); // 10

            using (mycaryardEntities dc = new mycaryardEntities())
            {
                List <PrivateChatMessage> msg = new List <PrivateChatMessage>();
                var v = (from a in dc.ChatPrivateMessageMasters
                         join b in dc.ChatPrivateMessageDetails on a.EmailID equals b.MasterEmailID into cc
                         from c in cc
                         where (c.MasterEmailID.Equals(fromid) && c.ChatToEmailID.Equals(toid)) || (c.MasterEmailID.Equals(toid) && c.ChatToEmailID.Equals(fromid))
                         orderby c.ID descending
                         select new
                {
                    UserName = a.UserName,
                    Message = c.Message,
                    ID = c.ID
                }).Take(takeCounter).Skip(skipCounter).ToList();

                foreach (var a in v)
                {
                    var res = new PrivateChatMessage()
                    {
                        userName = a.UserName,
                        message  = a.Message
                    };
                    msg.Add(res);
                }
                return(msg);
            }
        }
Exemple #3
0
        public List <PrivateChatMessage> GetPrivateMessage(string fromid, string toid, int take)
        {
            using (mycaryardEntities dc = new mycaryardEntities())
            {
                List <PrivateChatMessage> msg = new List <PrivateChatMessage>();

                var v = (from a in dc.ChatPrivateMessageMasters
                         join b in dc.ChatPrivateMessageDetails on a.EmailID equals b.MasterEmailID into cc
                         from c in cc
                         where (c.MasterEmailID.Equals(fromid) && c.ChatToEmailID.Equals(toid)) || (c.MasterEmailID.Equals(toid) && c.ChatToEmailID.Equals(fromid))
                         orderby c.ID descending
                         select new
                {
                    UserName = a.UserName,
                    Message = c.Message,
                    ID = c.ID
                }).Take(take).ToList();
                v = v.OrderBy(s => s.ID).ToList();

                foreach (var a in v)
                {
                    var res = new PrivateChatMessage()
                    {
                        userName = a.UserName,
                        message  = a.Message
                    };
                    msg.Add(res);
                }
                return(msg);
            }
        }
Exemple #4
0
 private void AddAllMessageinCache(string userName, string message)
 {
     using (mycaryardEntities dc = new mycaryardEntities())
     {
         var messageDetail = new ChatMessageDetail
         {
             UserName = userName,
             Message  = message,
             EmailID  = emailIDLoaded
         };
         dc.ChatMessageDetails.Add(messageDetail);
         dc.SaveChanges();
     }
 }
Exemple #5
0
        public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled)
        {
            using (mycaryardEntities dc = new mycaryardEntities())
            {
                var item = dc.ChatUserDetails.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
                if (item != null)
                {
                    dc.ChatUserDetails.Remove(item);
                    dc.SaveChanges();

                    var id = Context.ConnectionId;
                    Clients.All.onUserDisconnected(id, item.UserName);
                }
            }
            return(base.OnDisconnected(stopCalled));
        }
Exemple #6
0
        public void Connect(string userName, string email)
        {
            emailIDLoaded = email;
            var id = Context.ConnectionId;

            using (mycaryardEntities dc = new mycaryardEntities())
            {
                var item = dc.ChatUserDetails.FirstOrDefault(x => x.EmailID == email);
                if (item != null)
                {
                    dc.ChatUserDetails.Remove(item);
                    dc.SaveChanges();

                    // Disconnect
                    Clients.All.onUserDisconnectedExisting(item.ConnectionId, item.UserName);
                }

                var Users = dc.ChatUserDetails.ToList();
                if (Users.Where(x => x.EmailID == email).ToList().Count == 0)
                {
                    var userdetails = new ChatUserDetail
                    {
                        ConnectionId = id,
                        UserName     = userName,
                        EmailID      = email
                    };
                    dc.ChatUserDetails.Add(userdetails);
                    dc.SaveChanges();

                    // send to caller
                    var connectedUsers = dc.ChatUserDetails.ToList();
                    var CurrentMessage = dc.ChatMessageDetails.ToList();
                    Clients.Caller.onConnected(id, userName, connectedUsers, CurrentMessage);
                }

                // send to all except caller client
                Clients.AllExcept(id).onNewUserConnected(id, userName, email);
            }
        }
Exemple #7
0
        public void SendPrivateMessage(string toUserId, string message, string status)
        {
            string fromUserId = Context.ConnectionId;

            using (mycaryardEntities dc = new mycaryardEntities())
            {
                var toUser   = dc.ChatUserDetails.FirstOrDefault(x => x.ConnectionId == toUserId);
                var fromUser = dc.ChatUserDetails.FirstOrDefault(x => x.ConnectionId == fromUserId);
                if (toUser != null && fromUser != null)
                {
                    if (status == "Click")
                    {
                        AddPrivateMessageinCache(fromUser.EmailID, toUser.EmailID, fromUser.UserName, message);
                    }

                    // send to
                    Clients.Client(toUserId).sendPrivateMessage(fromUserId, fromUser.UserName, message, fromUser.EmailID, toUser.EmailID, status, fromUserId);

                    // send to caller user
                    Clients.Caller.sendPrivateMessage(toUserId, fromUser.UserName, message, fromUser.EmailID, toUser.EmailID, status, fromUserId);
                }
            }
        }