I am using Atmosphere framework 2.0.0.RC5 to extend my web application with websocket functionality and faced some strange error 'Websocket failed. Downgrading to Comet and resending', which I can't get away.
I used a websocket chat example as a my starting point: https://github.com/Atmosphere/atmosphere-samples/tree/master/samples/websocket-chat
The application has html+js client and a java backend.
Backend
- Tomcat 7.0.42 with NIO protocol enabled
- Web module v3.0 with Spring and Atmosphere servlets
- Custom CORS filter to allow atmosphere headers
- Every received message is logged by the server
(extra code omitted)
public void onTextMessage(WebSocket webSocket, String message) throws IOException {
logger.info("Message received: " + message);
webSocket.broadcast(mapper.writeValueAsString(mapper.readValue(message, Data.class)));
}
Client
index.html
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="js/lib/jquery.atmosphere-2.0.2.js"></script>
<script type="text/javascript" src="js/aimpl.js"></script>
aimpl.js
$(function() {
"use strict";
var socket = $.atmosphere;
var request = {
url: 'http://localhost:8181/services/echo',
transport: "websocket",
contentType: "application/json",
//connectTimeout: '300000',
fallbackTransport: 'long-polling',
enableXDR: true,
logLevel: 'debug'
};
request.onMessage = function (response) {
console.log(response.responseBody)
};
request.onOpen = function(response) {
console.log('Atmosphere connected using ' + response.transport);
};
request.onReconnect = function (request, response) {
console.log("reconnecting...");
};
request.onError = function(response) {
console.log('an error has occured.');
};
var channel = socket.subscribe(request);
channel.push(JSON.stringify({ author: 'me', message: 'hello from websocket!' }));
});
When I open a 'index.html' i see an error in the console (jquery.atmosphere-2.0.2.js line 1097) and no messages in server log:
Surprisingly it works when I type directly in the console:
And gets logged by the server, so I assume the server part is fine:
My guess it's a javascript issue, but I'm not 100% sure. I played with connectTimeout
parameter with no luck. Any ideas why it does not work in the script?
transport: "websocket",,
you have 2x ,, – Nummaryvar request = new $.atmosphere.AtmosphereRequest(); request.url = 'http://localhost:8181/services/echo';
etc.. – Nummary