Detecting Browser close's situation when using long polling - Atmosphere/atmosphere GitHub Wiki
When the transport is set to long-polling, the AtmosphereResourceEventListener#onDisconnect will be called when:
- the long-polling connection is resumed.
- 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.