Beispiel #1
0
        //public async Task SendMessage(string user, string message)
        //{
        //    await Clients.All.SendAsync("ReceiveMessage", user, message);
        //}

        public override async Task OnConnectedAsync()
        {
            //await Clients.All.SendAsync("ReceiveMessage", "User Connected " + Context.ConnectionId);

            //ActiveConnections.Add(Context.ConnectionId);

            if (Context.User.Identity.IsAuthenticated)
            {
                string id   = Context.User.FindFirst(ClaimTypes.NameIdentifier).Value;
                string role = Context.User.FindFirst(ClaimTypes.Role).Value;

                ObservedUser user = Users.FirstOrDefault(k => k.UserId == id);

                if (user == null)
                {
                    user = new ObservedUser()
                    {
                        UserId = id,
                    };

                    // add connection id
                    user.AddConnection(Context.ConnectionId);

                    // append to obs. users
                    Users.Add(user);
                }
                else
                {
                    // append connections
                    user.AddConnection(Context.ConnectionId);
                }

                // add log if not exist as login
                if (!ApplicationContext.ActiveLogs.Any(k => k.UserId == id))
                {
                    var ip = _context.HttpContext?.Connection?.RemoteIpAddress?.ToString();

                    var log = _userLogsMethods.AddStartLog(id, ip, DataContract.UserLogStatus.Login);

                    if (log != null)
                    {
                        ApplicationContext.ActiveLogs.Add(log);

                        await Clients.All.SendAsync("RefreshLogs");
                    }
                }

                //var cId = Context.User.FindFirst(ClaimTypes.NameIdentifier);
                //string id = cId.Value;

                //ApplicationContext.Methods.Users.AddStartLog(id, "");
            }

            await base.OnConnectedAsync();
        }
Beispiel #2
0
        public override async Task OnDisconnectedAsync(Exception exception)
        {
            //ActiveConnections.Remove(Context.ConnectionId);

            //await Clients.All.SendAsync("ReceiveMessage", "User Disconnected " + Context.ConnectionId);

            if (Context.User.Identity.IsAuthenticated)
            {
                string id   = Context.User.FindFirst(ClaimTypes.NameIdentifier).Value;
                string role = Context.User.FindFirst(ClaimTypes.Role).Value;

                ObservedUser user = Users.FirstOrDefault(x => x.HasConnection(Context.ConnectionId));

                if (user != null)
                {
                    // remove connectionid
                    user.RemoveConnection(Context.ConnectionId);


                    // no connections left
                    // this maybe a refresh of one page
                    // or closed the app
                    // we will wait n seconds to see if the user still disconnected
                    // then we will mark as disconnected
                    if (!user.IsActive)
                    {
                        await Task.Delay(5000).ContinueWith(k => CheckIfUserDisconnected(id));
                    }
                    //// if not active > disconnected
                    //if (!user.IsActive)
                    //{

                    //}
                }

                //if (user != null)
                //{
                //    Users.Remove(user);
                //}

                //if (Users.Any(x => x.ConnectionId == Context.ConnectionId))
                //{
                //    ObservedUser user = Users.First(x => x.ConnectionId == Context.ConnectionId);

                //    var cId = Context.User.FindFirst(ClaimTypes.NameIdentifier);
                //    string id = cId.Value;

                //    ApplicationContext.Methods.Users.EndLog(id, "");

                //}
            }

            await base.OnDisconnectedAsync(exception);
        }