I have been utilizing the Atmosphere framework 2.0.0.RC5 to expand my web application with websocket capabilities and encountered a perplexing error 'Websocket failed. Downgrading to Comet and resending' that I can't seem to resolve.
To start off, I referenced a websocket chat example: https://github.com/Atmosphere/atmosphere-samples/tree/master/samples/websocket-chat
The setup consists of an 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
- Logging every received message on the server side
(extra code excluded)
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!' }));
});
Upon opening 'index.html', I encounter an error in the console (jquery.atmosphere-2.0.2.js line 1097) and no messages are being logged on the server:
Interestingly, it functions when I manually type directly into the console:
The messages get logged by the server, so I am assuming the server side is functioning correctly:
I suspect it may be a JavaScript problem, though I'm not completely certain. I have tried adjusting the connectTimeout
parameter without success. Any insights as to why it's not working within the script?