Skip to content

Detecting Browser close's situation when using long polling

tehnicaorg edited this page Dec 30, 2013 · 5 revisions

When the transport is set to long-polling, the AtmosphereResourceEventListener#onDisconnect will be called when:

  1. the long-polling connection is resumed.
  2. when the browser's window/tab is closed or when the user move to another page.

To differentiate the two scenarios and act accordingly, the following is required

Client side

Enable the Atmosphere's protocol:

    var request = { url: document.location.toString() + 'chat',
        transport : 'long-polling' ,
        enableProtocol : true
    };
   
   request.onClose(response) {
        if (response.state == "unsubscribe") {
            // Tab/window closed
        }
   }

Server side

Inside your AtmosphereResourceEventListener#onDisconnect, do:

    @Override
    public void onDisconnect(AtmosphereResourceEvent event) {
        String transport = event.getResource().getRequest().getHeader(HeaderConfig.X_ATMOSPHERE_TRANSPORT);
        if (transport != null && transport.equalsIgnoreCase(HeaderConfig.DISCONNECT)) {
             // Scenario 2: Browser closed the connection
        } else {
             // Scenario 1: Long-Polling Connection resumed.
        }
    }

Other info

You can also install the SuspendTrackerInterceptor for unifying transport behavior.

Step by Step Tutorials

Concepts & Architecture

15 Minutes Tutorial

Advanced Topics

API

Known WebServer Issues

References

External Documentations

githalytics.com alpha

Clone this wiki locally