5

I'm new to Atmosphere framework & websockets.

I'm developing a single page app, As a first step I want to implement a basic authentication using AngularJs(Client side) and Atmosphere websockets(server side).

For authentication, in the beginning, I have used normal servlets and saved the User bean in HttpSession. But later I understood, I can't access the session attributes in the class(websocket service)

@AtmosphereService(broadcaster = JerseyBroadcaster.class)

So, I have created another Login AtmosphereService, In which suspend method checks the user and if user exists, then stores UserBean in ConcurrentMap against uuid key using following method so that I can access in future.

 public void createSession(AtmosphereResource resource) {
    logger.debug("Create session ({})", resource.uuid());
    ConcurrentMap<String, Object> session = new ConcurrentHashMap<String, Object>();
    ConcurrentMap<String, Object> prevSession = sessionsByUuid.putIfAbsent(resource.uuid(), session);
    if (prevSession != null) {
        logger.warn("Session already exists ({})", resource.uuid());
    }
}

So in the client side, As I have AngularJS and configured routes, when the user logged in, I store the user-key in a rootscope variable and when ever route changes it checks as follows.

 $rootScope.$on("$routeChangeStart", function(event, next, current) {
    aNext = next;
    if (next.templateUrl != "./partials/login.html") {
        if($rootScope.loggedUser !=null && $rootScope.loggedUser!=""){
           $location.path(nextPage);
        }else{
           $rootScope.goLoginPage();
        }
    }
});

But now the problem is If user refresh the page it goes to login page, because the $rootScope.loggedUser will not contain any thing.

Before, when I used simple servlets for authentication I used a ajax call to the servlet to check the user exists in the session or not and if he exists I assigned the user-key in $rootScope.loggedUser and route used to change properly.

Now as I'm using AtmosphereService instead of normal servlets for authentication, how can I make a request to either a servlet(in which I should access AtmosphereResource) or should I write another AtmosphereService to get the stored UserBean?

Siva Kumar
  • 715
  • 1
  • 7
  • 21

0 Answers0