GWT detect browser refresh in CloseHandler

2019-02-15 02:07发布

问题:

I have a GWT application and I want to run some code when the user leaves the application to force a logout and remove any data etc.

To do this I am using a CloseHandler and registering it using Window.addCloseHandler.

I have noticed that when the refresh button is clicked the onClose method is run but I have been unable to differentiate this event from a close where the user has closed the browser. If it is a refresh I do not want to do the logout etc, I only want to do this when the user closes the browser/tab or navigates away from the site.

Does anybody know how I can do this?

回答1:

There is no way to differentiate the 'close' from 'refresh'. But, you can set a cookie that holds the last CloseHandler call time and check, when loading the module, if this time is old enough to clean the information before showing the page.

You can do that with the folowing utility class (BrowserCloseDetector). Here is an example using it on the onModuleLoad.

The test lines:

@Override
public void onModuleLoad() {
    if (BrowserCloseDetector.get().wasClosed()) {
        GWT.log("Browser was closed.");
    }
    else {
        GWT.log("Refreshing or returning from another page.");
    }
}

The utility class:

import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;

public class BrowserCloseDetector {
    private static final String COOKIE = "detector";
    private static BrowserCloseDetector instance;

    private BrowserCloseDetector() {
        Window.addWindowClosingHandler(new Window.ClosingHandler() {
            public void onWindowClosing(Window.ClosingEvent closingEvent) {
                Cookies.setCookie(COOKIE, "");
            }
        });
    }

    public static BrowserCloseDetector get() {
        return (instance == null) ? instance = new BrowserCloseDetector() : instance;
    }

    public boolean wasClosed() {
        return Cookies.getCookie(COOKIE) == null;
    }
}


回答2:

Have you tried

<BODY onUnload = "scriptname">

in your gwt hosting/launching html file?

I am thinking that if you defined a map "hash" (i.e. a javascript pseudo hash) in the hosting file and then accessed the "hash" in GWT through Dictionary class, you could update values in that hash as the user progresses through the gwt app. Which means, your programming style would require you to log milestones on the user's progress onto this map.

When the user closes the browser page, the onunload script of the launching html page would be triggered. That script would access the map to figure out what needs to be updated to the server, or what other url to launch.



回答3:

I am intereted too if someone got a solution (GWT/java side only). Maybe we can do it with HistoryListerner ? 1-set a flag for your current viewing page. 2-when ClosingHandler event, launch a "timeout" on server-side (for example 10s) 3-if during this time your got a massage from HistoryListerner with the same last flag so it was just a refresh. of disconnect if timer is over...

Is not a good solution but I think it is easy to do... If someone have a better one...



标签: gwt refresh