GWT RequestFactory:在ClosingHandler中发送请求

尼齐尔

我正在使用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开发者工具上的状态已取消),因此请求失败
有解决这个问题的主意吗?

编辑:

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

托马斯·布罗耶

当您离开页面时,浏览器会取消正在进行的请求。因为是在关闭窗口时进行的,所以您甚至无法确定请求是通过网络发送并到达服务器的。没有解决方法。

一种可能(但也可能失败)的可能性是,打开一个新窗口,以便您可以在那里安全地发出请求,然后在完成后关闭该窗口。但是,这很可能会失败,因为此类窗口很可能会被浏览器的弹出窗口阻止程序(内置或附加组件)阻止。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章