0

Is there a way to detect when a user's click somewhere in our web app accesses to the server and when it does not? We need this knowledge for a simple keep alive mechanism we want to add to the app. I have already implemented the functionality to detect user clicks anywahere in page, using help from this post: Click everywhere but here event. And now we need to add functionality to detect when the click causes an access to server and when it doesn't (because we use two timers in our keep alive, one for server session and one for client session).
The backend is implemented in ASP.NET Core Web API.

Thanks,
ashilon

ashilon
  • 1,791
  • 3
  • 24
  • 41

1 Answers1

1

The least invasive solution I can think of would be to add a request or response interceptor to the $http service that would track the time of the last event. It might look something like this, but obviously would have to be modified for the particulars of your angularjs app.

angular.module('yourAppName').
    service('SessionInterceptor', 
    ['$q', '$rootScope', 
    function($q, $rootScope) {
        'use strict';

        return {
            response: function(response) {
                $rootScope.lastServerActivity = response.headers().date;
                return response;
            },
            responseError: function(rejection) {
                $rootScope.lastServerActivity = rejection.headers().date;
                return $q.reject(rejection);
            }
        };
    }]);

Then you can use the lastServerActivity to calculate an idle time on their next click or via a timer and kill the session if appropriate. You could just as easily implement the request side rather than the response side. You could also use javascript to get the date rather than from the http headers. Whatever you choose will be influenced by how you are doing the client side session timeouts.

tokkov
  • 2,959
  • 1
  • 13
  • 9