Search code examples
gwtsmartgwtgwt-extgwt-platform

gwt client session time out


I am using gwt 2.3 with gwtp framework.In this application I wan to maintain a session time of 5 mins.This means if current user is not doing up to 5 min and he comes after five min then on his first event/action on screen a he should be be logged out. In gwt there is class named Timer which can be used in this issues.But I am not getting how to recognize action of user on the screen.I did google on it, & found the code for gwt-ext.Below is the code of gwt-ext

Ext.get(“pagePanel”).addListener(“click”, new EventCallback() {
    @Override
    public void execute(EventObject e) {
        MessageBox.alert(“On Mouse Click”);
    }
});

Ext.get(“pagePanel”).addListener(“keydown”, new EventCallback() {

    @Override
    public void execute(EventObject e) { 
        MessageBox.alert(“On Key Press Click”);
    }
});

In above code tag in working properly so I am attaching link from where I got this code.here

Same type of code I am looking in gwt.If there any other better way to do this then please let me know. Thanks in advance


Solution

  • If action/event can be really everythin, I would solve it with a NativePreviewHandler in the following way:

    boolean expired;
    
    final Timer logoutTimer = new Timer() {
        @Override
        public void run() {
            expired = true;
        }
    };
    
    NativePreviewHandler nph = new NativePreviewHandler() {
    
        @Override
        public void onPreviewNativeEvent(NativePreviewEvent event) {
            if (!expired) {
                logoutTimer.cancel();
                logoutTimer.schedule(300000);
            } else {
                // do your logout stuff here
            }
        }
    };
    
    Event.addNativePreviewHandler(nph);
    

    If the user shell be logged out without a new action after 5 minutes:

    final Timer logoutTimer = new Timer() {
        @Override
        public void run() {
            // do your logout stuff here
        }
    };
    
    NativePreviewHandler nph = new NativePreviewHandler() {
    
        @Override
        public void onPreviewNativeEvent(NativePreviewEvent event) {
            // Of course do this only when logged in:
            logoutTimer.cancel();
            logoutTimer.schedule(300000);
        }
    };
    
    Event.addNativePreviewHandler(nph);