0

我正在使用 RequestFactory 框架为基于 GWT 的应用程序开发审计服务。我在使用ClosingHandler. 这是我的代码:

我的审计服务总结:

private static final int MAX_CACHE_SIZE = 15;
private int cacheSize = 0;
private AuditServiceRequestContext context;

@Override
public void audit(String event, String details) {
    if (context == null)
        context = createContext();
    AuditServiceRequestContext cxt = createContext();
    context.append(cxt);

    AuditProxy proxy = cxt.create(AuditProxy.class);
    /* intialize the proxy with event and details */
    cxt.persist(proxy);

    if (++cacheSize >= MAX_CACHE_SIZE)
        flush();
}

public void flush() {
    context.fire();
    cacheSize = 0;
    context = null;
}

我目前如何处理注销事件:

Window.addWindowClosingHandler(new ClosingHandler() {
                        @Override
                        public void onWindowClosing(ClosingEvent event) {
                            audit.audit("logout", "the user has closed the app");
                            audit.flush();
                        }
                        });

数据被保留,但请求失败,因为 HTTP 请求/gwtRequest未返回任何响应(在 chrome 的开发人员工具上已取消状态)。
有什么想法可以解决这个问题吗?

编辑:

CloseHandler奇怪的是,使用with没有错误Window#addCloseHandler(CloseHandler)。不明白为什么,但它有效(如果有人可以向我解释,我真的很喜欢):D

4

1 回答 1

3

When you're navigating away from the page, the browser cancels ongoing requests. Because you make yours at window closing, you cannot even be sure the request was sent over the wire and reached your server. There's no workaround.

One possibility, but which is likely to fail too, is to open a new window so you can safely make requests there, and then close that window when you're done. It's likely to fail however as such windows are likely to be blocked by browsers' popup blockers (built-in or addons).

于 2013-07-12T09:11:02.050 回答