Пример #1
0
        public override Task OnDisconnected(bool stopCalled)
        {
            //var a = GlobalHost.Configuration.ConnectionTimeout;
            //var b = GlobalHost.Configuration.DisconnectTimeout;
            //var c = GlobalHost.Configuration.KeepAlive;

            if (stopCalled)
            {
                if (StaffClients.Any(x => x.ConnectionID == Context.ConnectionId))
                {
                    StaffClient user = StaffClients.First(x => x.ConnectionID == Context.ConnectionId);

                    //xoa khoi danh sach cac user dang dang nhap
                    StaffClients.RemoveAll(x => x.ConnectionID == Context.ConnectionId);

                    user.ConnectionID = null;
                    int  count          = 5;
                    bool isDisconnected = true;
                    while (count > 0)
                    {
                        //user = Users.First(x => x.ConnectionID == Context.ConnectionId);
                        Clients.All.addNewMessageToPage(user.UserLoggedId.ToString(), "ConnectionID: " + user.ConnectionID + "; Context.ConnectionId: " + Context.ConnectionId);
                        if (string.IsNullOrEmpty(user.ConnectionID))
                        {
                            Thread.Sleep(1000);
                            count--;
                        }
                        else
                        {
                            isDisconnected = false;
                            break;
                        }
                    }

                    if (isDisconnected)
                    {
                        Clients.Others.userLeft(user.UserLoggedId);
                        StaffClients.Remove(user);
                    }
                }
            }

            return(base.OnDisconnected(stopCalled));
        }
Пример #2
0
        public override Task OnConnected()
        {
            //lấy list user đang đăng nhập
            var userLoggedId = Helpers.Common.CurrentUser.Id;

            //lấy nhân viên dựa vào user.
            //var GetStaffByUserId = Helpers.Common.GetStaffByUserId(userLoggedId);
            //var staffId = GetStaffByUserId == null ? 0 : GetStaffByUserId.Id;
            if (StaffClients.Where(x => x.UserLoggedId == userLoggedId).FirstOrDefault() == null)
            {
                StaffClient user = new StaffClient()
                {
                    Name = Context.User.Identity.Name, UserLoggedId = userLoggedId
                };
                StaffClients.Add(user);
                Clients.Others.userConnected(user.UserLoggedId);
            }
            return(base.OnConnected());
        }