Mouse movement for session timeout in GWT

假如想象 提交于 2019-12-06 07:25:21

You can do code something like this on on module load.

Create one timer which is running every 2 minute and set session expire flage:

    private static Timer SESSION_TIMER = new Timer() {
            public void run() {
                SessionFactory.putValue(SesisonKey.SESSION_EXPIRED,
                        true);
            }
        };

call this method for Renew the timer session as below in on module load using this method:

    public static void renewTimer() {
            if (CLIENT_SIDE_TIMER != null) {
                CLIENT_SIDE_TIMER.cancel();
                SessionFactory.getClientInstance().put(
                        SesisonKey.SESSION_EXPIRED, false);
                //CLIENT_SIDE_TIMER.schedule(250 * 60 * 20);
                //1 Minute = 60000 Milliseconds
                CLIENT_SIDE_TIMER.schedule(60000 * 2);
            }
        }

NativePreviewHandler handler catch on mouse event and check that session is expired or not flag.

 final NativePreviewHandler nativeHandler = new NativePreviewHandler() {
                    @Override
                    public void onPreviewNativeEvent(NativePreviewEvent event) {

                    preventBack();

                        if (SessionFactory
                                .getValue(SesisonKey.CLIENT_SESSION_EXPIRED) != null) {
                            boolean expire = (Boolean) SessionFactory
                                    .getValue(SesisonKey.CLIENT_SESSION_EXPIRED);
                            if (expire) {
                                boolean show= false;
                                //logout session code
                                ClientSideTimers.renewSessionTimer();
                            }
                        }
                    }
                };
                Event.addNativePreviewHandler(nativeHandler);

if your screen stable for 2 minute than it will set expire session variable value false through timer and if value is false then do logout.

SESSION_EXPIRED is a constant variable for session expired check not more that that SessionFactory is my custom factory code. U just ignore that used varibles which my project pattern. u have to just set one constant only not more than that. Let me know if still any query.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!