{Java} Vaadin 14-检测用户休假(关闭标签页,f5等) [英] {Java} Vaadin 14 - Detect user leave (closes tab, f5, etc)

查看:143
本文介绍了{Java} Vaadin 14-检测用户休假(关闭标签页,f5等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用 Vaadin Flow版本14 (

I'm currently using Vaadin Flow version 14 (https://github.com/vaadin/platform/releases/tag/14.0.0)
I run Java version 1.8.0_231, 64 bit.

只要用户执行以下任一操作,我只是想能够检测到(在Java中!):

I simply want to be able to detect (in java!) whenever a user does either of these actions:

  • 关闭当前浏览器标签(或浏览器)
  • 在键盘上单击F5,或在浏览器中按下刷新"按钮
  • 点击链接(或其他任何链接),将其重定向到我的网站之外

我已经尝试了许多不同的方法来检测到这一点.到目前为止,我唯一能够检测到的就是每当当前的VaadinSession过期时(可以通过执行VaadinSession.getCurrent().getSession().setMaxInactiveInterval(15)进行更改).这样一来,每次会话都会在15秒后过期(在我的情况下,15仅是一个测试数字).

I've tried lots of different things to detect this. The only thing I have been able to detect so far, is whenever the current VaadinSession expires (which I can change by doing VaadinSession.getCurrent().getSession().setMaxInactiveInterval(15)). This makes every session expire after 15 seconds (15 is just a testing number in my case).



相信通过 Vaadin 8 ,您可以做到这一点,并且可以立即使用:



With Vaadin 8, I believe you can just do this and it will work out of the box:

        JavaScript.getCurrent().execute(
            "function closeListener() { catchClose(); } " +
                    "window.addEventListener('beforeunload', closeListener); " +
                    "window.addEventListener('unload', closeListener);"
        );
        JavaScript.getCurrent().addFunction("catchClose", arguments ->
        {
            System.out.println("user has quit :o");
        });

我不能使用此方法,因为我使用的是 Vaadin 14 ,而不是 8 .


我尝试将其添加到我的View类中:

I can't use this tho, since I use Vaadin 14, not 8.


I have tried adding this to my View class:

    @Override
    protected void onDetach(DetachEvent detachEvent)
    {
        System.out.println("onDetach " + detachEvent);
    }

它仅在会话期满(在我的情况下为15秒)时打印此消息.即使我不关闭页面.


我已经尝试实现 BeforeLeaveObserver / BeforeLeaveListener 并覆盖了这一点:

It only prints this message when the session expires (15 seconds in my case). Even if I don't close the page.


I have tried implementing BeforeLeaveObserver / BeforeLeaveListener and overridding this:

    @Override
    public void beforeLeave(BeforeLeaveEvent beforeLeaveEvent)
    {
        System.out.println("beforeLeave");
    }

从不打印.


我也尝试了所有这些方法,但是它们并不能满足我的需求:

This never prints.


I've also tried all of these, but they do not do what I need:

        VaadinResponse.getCurrent().getService().addUIInitListener(e ->
        {
            System.out.println("1 addUIInitListener : " + e);
            e.getUI().addBeforeLeaveListener(e2 ->
            {
                System.out.println("1.1 addBeforeLeaveListener : " + e2);
            });
            e.getUI().addDetachListener(e2 ->
            {
                System.out.println("1.2 addDetachListener : " + e2);
            });
        });
        VaadinResponse.getCurrent().getService().addServiceDestroyListener(e ->
        {
            System.out.println("2 addServiceDestroyListener : " + e);
        });
        VaadinResponse.getCurrent().getService().addSessionDestroyListener(e ->
        {
            System.out.println("3 addSessionDestroyListener : " + e);
        });

1 addUIInitListener在用户加载页面时被打印.
1.1 addBeforeLeaveListener从不打印.
2 addServiceDestroyListener从不打印.服务与会话不同.很有道理.
1.23 addSessionDestroyListener会稍后打印.大约需要15-30秒.


基本上没有办法立即检测到这种情况吗? :/

1 addUIInitListener gets printed when the user loads the page.
1.1 addBeforeLeaveListener never prints.
2 addServiceDestroyListener never prints. A Service isn't the same as a Session. Makes sense tho.
1.2 and 3 addSessionDestroyListener prints wafter awhile. Takes like 15-30 seconds tho.


Is there not a way to detect this basically instantly? :/

推荐答案

根据您需要支持的浏览器,您可能还想看看

Depending on which browsers you need to support, you might also want to take a look at the Beacon API, which is supported by all modern browsers, but not IE11.

Beacon API的优点是不受阻塞.使用卸载侦听器,客户端在关闭选项卡之前等待响应,这可能会带来糟糕的用户体验.

The Beacon API has the benefit of being non-blocking. With the unload listener, the client waits for a response before closing the tab, which might be bad user experience.

正如Tatu所说,如果浏览器崩溃或用户失去互联网连接,您所能做的就是等待会话超时.

Still, as Tatu mentioned, if the browser crashes or the user loses internet connection, all you can do is wait for the session to time out.

这篇关于{Java} Vaadin 14-检测用户休假(关闭标签页,f5等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆