I have the following code in the onModuleLoad() of my application:
Window.addWindowClosingHandler(new ClosingHandler() {
@Override
public void onWindowClosing(ClosingEvent event) {
event.setMessage("If you choose to close, application will sign out");
}
});
//sign out on close
Window.addCloseHandler(new CloseHandler<Window>() {
@Override
public void onClose(CloseEvent<Window> event) {
sendLogout();
}
});
The sendLogout() function looks like this:
// Set up the callback object.
AsyncCallback<String> callback = new LogoutCallback(this);
// Make the call to the survey service.
SurveySystemService.Util.getInstance().logout(details, callback);
Where ‘details’ is some object.
It works just fine when the window is closed, but if I try to refresh the page, it doesn’t log out. What I figured is that since the call is asynchronous, it doesn’t finish getting the message off to the server before the module is restarted.
I’ve tried:
1. creating and calling the callback inside the onClose method.
2. using a Timer to check if the call was made.
3. Endless loos which check the same as the above (I got desperate).
In all of these solutions, the program would reach the callback creation, but the server never received anything.
any help with this?
Can you just call logout any time the page first loads. Due to the stateless nature of the web the GWT application will not know the difference between someone hitting refresh or just navigating to the page.
You can store an ID variable in session storage which should be maintained until the browser window or tab is closed. If on the application start the ID variable exists in session storage you can use it to trigger the log out.