我使用Atmosphere框架2.0.0.RC5扩展我的web应用程序与websocket功能,并遇到一些奇怪的错误'Websocket失败。降级为Comet并重新发送',我无法逃脱。
我使用websocket聊天示例作为我的起点:https://github.com/Atmosphere/atmosphere-samples/tree/master/samples/websocket-chat
该应用程序具有html + js客户端和java后端。
后端
(省略额外代码)
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
<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!' }));
});
当我打开'index.html'时,我在控制台中看到错误(jquery.atmosphere-2.0.2.js第1097行),服务器日志中没有消息:
令人惊讶的是,当我直接在控制台中输入时,它可以正常工作:
并由服务器记录,所以我假设服务器部分没问题:
我猜这是一个javascript问题,但我不是百分百肯定。我玩connectTimeout
参数没有运气。任何想法为什么它在脚本中不起作用?