Saturday, June 26, 2010

Re: Sessions and GWT

Hellos!!!

I have the solution you searching for:

check this code: :D

/**
 *
 */
import java.util.Date;

import pt.quantum.padroes.client.services.LoginService;
import pt.quantum.padroes.client.services.LoginServiceAsync;
import pt.quantum.padroes.client.util.LogUtils;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * @author brunolopes
 *
 */
public class UserIdleTimeOutControl {

        Timer mInactivityTimer;
        HandlerRegistration mHandlerRegistration;
        int mTimeoutInMillis = 5*60000;//thirty minutes max user inactivity
       
        public UserIdleTimeOutControl() {

            mInactivityTimer = new Timer() {
                @Override
                public void run() {
                    // Logout
                    mHandlerRegistration.removeHandler();
                    mInactivityTimer.cancel();
                    LoginServiceAsync service = GWT.create(LoginService.class);
                    service.logout(new AsyncCallback<Void>() {
                        @Override
                        public void onFailure(Throwable caught) {
                            LogUtils.error(caught.getMessage());
                            LogUtils.loggingOut();
                        }

                        @Override
                        public void onSuccess(Void result) {
                            LogUtils.info("User idle for "+ (mTimeoutInMillis/1000)/60 +" minutes" +
                                    " logout occur : " + new Date());
                            LogUtils.loggingOut();
                        }
                    });
                  
                }
            };
            mInactivityTimer.schedule(mTimeoutInMillis);

            NativePreviewHandler handler;
            handler = new NativePreviewHandler() {
                @Override
                public void onPreviewNativeEvent(NativePreviewEvent event) {
                    mInactivityTimer.schedule(mTimeoutInMillis);
                    LogUtils.info("User Event fired occur: " + new Date());
                    
                }
            };
            mHandlerRegistration = Event.addNativePreviewHandler(handler);
        }
    }


Basically it listens all the interface events and if no event occurs in X minutes session invalidate and logout will be called.

Is something like this you looking for ?

Hope it helps! :

Bruno

On Sat, Jun 26, 2010 at 2:37 PM, andrew_d_mackenzie <andrew@mackenzie-serres.net> wrote:
Thanks for posing the question, it seems to be a recurring one and I
have the same doubts.

I find it slightly surprising that there is no standard answer, or
tutorial on the subject....

More pointers or descriptions of how others have covered this need
would I think be appreciated by many!

Google?

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment