When the preferred transport fails, atmosphere tries to use the fallback transport. It tried to reconnect maxReconnect times. After that it calls onClose and onError.
When I try the subscribe again Atmosphere always uses the fallback transport for the connection.
Here is my client configuration:
AtmosphereRequestConfig jsonRequestConfig = // ...
jsonRequestConfig.setTransport(AtmosphereRequestConfig.Transport.WEBSOCKET);
jsonRequestConfig.setFallbackTransport(AtmosphereRequestConfig.Transport.LONG_POLLING);
jsonRequestConfig.setLogLevel("debug");
jsonRequestConfig.setMaxReconnectOnClose(1);
Atmosphere atmosphere = Atmosphere.create();
clientRequest = atmosphere.subscribe(jsonRequestConfig);
When my server is running and the client connects the first time (page reload) the connection is on Websockets. Then I stopped my server the client shows this:
atmosphere.js:3252 Sat Jan 30 2016 22:17:14 GMT+0100 (CET) Atmosphere: websocket.onclose
atmosphere.js:3252 Websocket closed, reason: Normal closure; the connection successfully completed whatever purpose for which it was created. - wasClean: true
atmosphere.js:3252 Sat Jan 30 2016 22:17:14 GMT+0100 (CET) Atmosphere: Firing onClose (closed case)
ConsoleLogger.java:32 AtmosphereListener: onClose
atmosphere.js:3252 Sat Jan 30 2016 22:17:14 GMT+0100 (CET) Atmosphere: Request already closed, not firing onClose (closed case)
atmosphere.js:3252 Sat Jan 30 2016 22:17:14 GMT+0100 (CET) Atmosphere: invoking .close() on WebSocket object
atmosphere.js:3252 Sat Jan 30 2016 22:17:14 GMT+0100 (CET) Atmosphere: Firing onReconnect
ConsoleLogger.java:32 AtmosphereListener: onReconnect
atmosphere.js:3252 Sat Jan 30 2016 22:17:14 GMT+0100 (CET) Atmosphere: Firing onReconnect
atmosphere.js:3252 Invoking executeWebSocket, using URL: ws://www.example.com:8080/socket/widget/2418C3F0-9A9B-48C4-8EE3-0541465EEACD%7C%7Crefdgdfgwe324234%7C%7CAF8F7A67-CDD0-4AD3-96C4-D447E970D0F8?X-Atmosphere-tracking-id=f89f00d8-b412-4825-80f9-ac8ca280edb5&X-Atmosphere-Framework=2.3.1-javascript&X-Atmosphere-Transport=websocket&Content-Type=application/json; charset=UTF-8&X-atmo-protocol=true
atmosphere.js:3252 Sat Jan 30 2016 22:17:14 GMT+0100 (CET) Atmosphere: websocket.onopen
atmosphere.js:3252 Websocket successfully opened
atmosphere.js:3252 Sat Jan 30 2016 22:17:15 GMT+0100 (CET) Atmosphere: websocket.onclose
atmosphere.js:3252 Websocket closed, reason: Connection was closed abnormally (that is, with no close frame being sent). - wasClean: false
atmosphere.js:3252 Sat Jan 30 2016 22:17:15 GMT+0100 (CET) Atmosphere: Request already closed, not firing onClose (closed case)
atmosphere.js:3252 Sat Jan 30 2016 22:17:15 GMT+0100 (CET) Atmosphere: Request already closed, not firing onClose (closed case)
atmosphere.js:3252 Sat Jan 30 2016 22:17:15 GMT+0100 (CET) Atmosphere: invoking .close() on WebSocket object
atmosphere.js:3252 Websocket reconnect maximum try reached 2
atmosphere.js:3252 Websocket error, reason:
atmosphere.js:3252 Sat Jan 30 2016 22:17:15 GMT+0100 (CET) Atmosphere: Firing onError, reasonPhrase: maxReconnectOnClose reached
Then I restarted my server. The client automatically reconnects because he tries to init the atmosphere connection in onError
. Here is the log I get:
atmosphere.js:1195 WebSocket connection to 'ws://www.example.com:8080/socket/widget/2418C3F0-9A9B-48C4-8EE3-0541465EEACD%7C%7Crefdgdfgwe324234%7C%7CAF8F7A67-CDD0-4AD3-96C4-D447E970D0F8?X-Atmosphere-tracking-id=0&X-Atmosphere-Framework=2.3.1-javascript&X-Atmosphere-Transport=websocket&Content-Type=application/json;%20charset=UTF-8&X-atmo-protocol=true' failed: Error during WebSocket handshake: Unexpected response code: 302
atmosphere.js:3252 Sat Jan 30 2016 22:22:43 GMT+0100 (CET) Atmosphere: websocket.onerror
atmosphere.js:3252 Sat Jan 30 2016 22:22:43 GMT+0100 (CET) Atmosphere: websocket.onclose
atmosphere.js:3252 Websocket closed, reason: Connection was closed abnormally (that is, with no close frame being sent). - wasClean: false
atmosphere.js:3252 Sat Jan 30 2016 22:22:43 GMT+0100 (CET) Atmosphere: Firing onClose (closed case)
ConsoleLogger.java:32 AtmosphereListener: onClose
atmosphere.js:3252 Sat Jan 30 2016 22:22:43 GMT+0100 (CET) Atmosphere: Request already closed, not firing onClose (closed case)
atmosphere.js:3252 Websocket failed on first connection attempt. Downgrading to long-polling and resending
ConsoleLogger.java:32 AtmosphereListener: onTransportFailure: Websocket failed on first connection attempt. Downgrading to long-polling and resending
ConsoleLogger.java:32 AtmosphereListener: onTransportFailure: Websocket failed on first connection attempt. Downgrading to long-polling and resendingmhi_g$ @ ConsoleLogger.java:32Hgi_g$ @ SimpleConsoleLogHandler.java:36cgi_g$ @ Logger.java:262bgi_g$ @ Logger.java:250ugi_g$ @ Logger.java:178tgi_g$ @ Logger.java:162Agi_g$ @ Logger.java:129pNi_g$ @ AtmosphereListener.java:114(anonymous function) @ AtmosphereRequestConfig.java:389shc_g$ @ Impl.java:239vhc_g$ @ Impl.java:291(anonymous function) @ Impl.java:77_reconnectWithFallbackTransport @ atmosphere.js:1729_websocket.onclose @ atmosphere.js:1534
atmosphere.js:3252 Sat Jan 30 2016 22:22:43 GMT+0100 (CET) Atmosphere: ajaxRequest.onreadystatechange, new state: 2
2atmosphere.js:3252 Sat Jan 30 2016 22:22:43 GMT+0100 (CET) Atmosphere: ajaxRequest.onreadystatechange, new state: 3
atmosphere.js:2117 XHR finished loading: GET "http://www.example.com:8080/socket/widget/2418C3F0-9A9B-48C4-8…plication%2Fjson%3B%20charset%3DUTF-8&X-atmo-protocol=true&_=1454188963647"._executeRequest @ atmosphere.js:2117_execute @ atmosphere.js:644_reconnectWithFallbackTransport @ atmosphere.js:1745_websocket.onclose @ atmosphere.js:1534
atmosphere.js:3252 Sat Jan 30 2016 22:22:43 GMT+0100 (CET) Atmosphere: ajaxRequest.onreadystatechange, new state: 4
atmosphere.js:3252 Sat Jan 30 2016 22:22:43 GMT+0100 (CET) Atmosphere: ajaxRequest.onreadystatechange, new state: 2
atmosphere.js:3252 Sat Jan 30 2016 22:22:43 GMT+0100 (CET) Atmosphere: Firing onOpen
The log says:
Websocket failed on first connection attempt. Downgrading to long-polling and resending
How do I force Atmosphere.js to use the preferred transport (Websockets) instead of the fallback transport (Long Polling) when the server was restarted?