0

I'm looking for a solution to detect closing a BrowserTab window.

I use the annotation @Push. As a result, the web socket connection is always closed as soon as the Borwser closes or the page is reloaded by F5.

If the web socket connection is terminated, the AtmospherePushConnection.connectionLost() method is called. I would like a listener if the value of AtmospherePushConnection.State changes.

Is there a solution for this?

Samuel
  • 1
  • I don't think that this is possible: https://stackoverflow.com/questions/3888902/detect-browser-or-tab-closing – Simon Martinelli Jul 23 '20 at 12:15
  • The mechanism of Websocket shows whether the browser is closed. A Websocket connection create a TCP connection. If the Browser is closed, the TCP is closed too... I am looking for that listener on this websocket connection for Vaadin... – Samuel Jul 23 '20 at 12:25
  • There is a way to listen to the `beforeunload` event, I'm using that to ask the user if he wants to save his unsaved changes before leaving. but I'm not too sure if that meets your criteria. https://vaadin.com/forum/thread/17523194/unsaved-changes-detect-page-exit-or-reload – kscherrer Jul 24 '20 at 06:53

1 Answers1

0

You can detect the event for browser close or page reload with onDetach method.

For E.g.,:

@Override
protected void onDetach(DetachEvent detachEvent) {
    // Your custom code here
}
  • 1
    Welcome to Stack Overflow. It will always be better to provide an example, rather than just give the name of a method or library that could help. Remember that you're not just answering to help the OP, but you may help hundreds of others in the future with a good, complete answer. To provide an example, click [edit] under your answer. – David Buck Sep 26 '20 at 10:03