我使用Atmosphere框架2.0.0.RC5扩展我的Web应用程序与websocket功能,并遇到一些奇怪的错误’Websocket失败.降级为Comet并重新发送’,我无法逃脱.
该应用程序具有html js客户端和java后端.
后端
>启用了NIO协议的Tomcat 7.0.42
>带有Spring和Atmosphere servlet的Web模块v3.0
>自定义CORS过滤器,允许大气标头
>服务器会记录每条收到的消息
(省略额外代码)
public void onTextMessage(WebSocket webSocket, String message) throws IOException {
logger.info("Message received: " + message);
webSocket.broadcast(mapper.writeValueAsString(mapper.readValue(message, Data.class)));
}
客户
的index.html
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!' }));
});
当我打开’index.html’时,我在控制台中看到错误(jquery.atmosphere-2.0.2.js第1097行),服务器日志中没有消息:
当我直接在控制台中输入时,它很有用:
并由服务器记录,所以我假设服务器部分正常:
我猜这是一个javascript问题,但我不是100%肯定.我玩connectTimeout参数没有运气.任何想法为什么它在脚本中不起作用?