4

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?

Michael
  • 32,527
  • 49
  • 210
  • 370

1 Answers1

-1

How do I force Atmosphere.js to use the preferred transport (Websockets) instead of the fallback transport (Long Polling) when the server was restarted?

Atmosphere client automatically attempts to reconnect using the fallbackTransport. That is the default behaviour.

To force it to reconnect using the preferred transport you might have to change the default behaviour.

It's possible to change that by commenting out this line in atmosphere.js see here.

This will ensure that the preferred web-socket transport is always used to when reconnecting and the fallbackTransport will not used.

_websocket.onclose = function(webSocketOpened) {
//...
if (_abortingConnection) {
    atmosphere.util.log(_request.logLevel, ["Websocket closed normally"]);
/** remove if you never want to use fallback transport
} else if (!webSocketOpened) {
    _reconnectWithFallbackTransport("Websocket failed on first connection attempt. Downgrading to " + _request.fallbackTransport + " and resending");
*/
} else if (_request.reconnect && _response.transport === 'websocket' ) {
    _clearState();
    if (_requestCount++ < _request.maxReconnectOnClose) {
        _open('re-connecting', _request.transport, _request);
        if (_request.reconnectInterval > 0) {
            _request.reconnectId = setTimeout(function () {
                _response.responseBody = "";
                _response.messages = [];
                _executeWebSocket(true);
            }, _request.reconnectInterval);
        } else {
            _response.responseBody = "";
            _response.messages = [];
            _executeWebSocket(true);
        }
    }
} else {
        atmosphere.util.log(_request.logLevel, ["Websocket reconnect maximum try reached " + _requestCount]);
        if (_canLog('warn')) {
            atmosphere.util.warn("Websocket error, reason: " + message.reason);
        }
        _onError(0, "maxReconnectOnClose reached");
    }
}

Another way would be to make use of onReconnect callback to switch back the clientRequest.request parameters on the fly.

Why?

Because when the connection is closed and onTransportFailure is invoked. Atmosphere changes the transport to the fallbackTransport and can eventually end up = 'none' (if it was set to websocket).

Assuming your request includes the following config :

  request.reconnectOnServerError= true,
  request.executeCallbackBeforeReconnect= true; 

you can add this to your onReconnect callback :

   clientRequest.request.transport = 'websocket'; // force preferred transport on reconnect
   //clientRequest.request.fallbackTransport = 'websocket'; // optional

The problem is if you set the fallback to websocket it will only attempt to reconnect once where as if you leave it blank it will still use web-socket protocol provided you used a url( starting with ws:// or wss://) and not webSocketUrl.

request.url = 'ws://example.com/ws'; 
Yohannes
  • 115
  • 5