Ejemplo n.º 1
0
        public void Connect(string userName, string email)
        {
            emailIDLoaded = email;
            var id = Context.ConnectionId;

            using (Service.EntityModel.DuAnYTeEntities dc = new Service.EntityModel.DuAnYTeEntities())
            {
                var thanhvien = dc.ThanhViens.FirstOrDefault(x => x.Email == email);
                var item      = dc.ChatUserDetails.FirstOrDefault(x => x.id_ThanhVien == thanhvien.id);
                //var k = dc.ThanhViens.FirstOrDefault(x => x.Email == email);
                if (item != null)
                {
                    dc.ChatUserDetails.Remove(item);
                    dc.SaveChanges();

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

                var Users = dc.ChatUserDetails.ToList();
                if (Users.Where(x => x.id_ThanhVien == thanhvien.id).ToList().Count == 0)
                {
                    var userdetails = new ChatUserDetail
                    {
                        ConnectionId = id,
                        id_ThanhVien = thanhvien.id
                    };
                    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);
                    List <ChatUserDetail> final = new List <ChatUserDetail>();
                    foreach (var itemtemp in connectedUsers)
                    {
                        ChatUserDetail temp = new ChatUserDetail();
                        temp.id           = itemtemp.id;
                        temp.id_ThanhVien = itemtemp.id_ThanhVien;
                        temp.ConnectionId = itemtemp.ConnectionId;
                        final.Add(temp);
                    }
                    //Clients.Caller.onConnected(id, userName, connectedUsers);
                    Clients.Caller.onConnected(id, userName, final);
                }

                // send to all except caller client
                Clients.AllExcept(id).onNewUserConnected(id, userName, email);
            }
        }
Ejemplo n.º 2
0
        //private void AddAllMessageinCache(string userName, string message)
        //{
        //    using (Service.EntityModel.DuAnYTeEntities dc = new Service.EntityModel.DuAnYTeEntities())
        //    {
        //        var messageDetail = new ChatMessageDetail
        //        {
        //            UserName = userName,
        //            Message = message,
        //            EmailID = emailIDLoaded
        //        };
        //        dc.ChatMessageDetails.Add(messageDetail);
        //        dc.SaveChanges();
        //    }
        //}

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

                // Save details
                var thanhviengui  = dc.ThanhViens.FirstOrDefault(x => x.Email == fromEmail);
                var thanhviennhan = dc.ThanhViens.FirstOrDefault(x => x.Email == chatToEmail);
                var resultDetails = new ChatPrivateMessageDetail
                {
                    id_ThanhVien_Gui  = thanhviengui.id,
                    id_ThanhVien_Nhan = thanhviennhan.id,
                    NoiDung           = message
                };
                dc.ChatPrivateMessageDetails.Add(resultDetails);
                dc.SaveChanges();
            }
        }
Ejemplo n.º 3
0
        public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled)
        {
            using (Service.EntityModel.DuAnYTeEntities dc = new Service.EntityModel.DuAnYTeEntities())
            {
                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.id_ThanhVien);
                }
            }
            return(base.OnDisconnected(stopCalled));
        }