我正在尝试使用Javascript websockets连接到Java servlet,但是onError函数总是被调用,并且没有提供太多信息。我的javascript:
var webSocket = new WebSocket('ws://localhost:8080/test');
webSocket.onerror = function(event) {
onError(event);
};
webSocket.onclose = function(event) {
onClose(event);
};
webSocket.onopen = function(event) {
onOpen(event);
};
webSocket.onmessage = function(event) {
onMessage(event);
};
function onClose(event) {
var code = event.code;
var reason = event.reason;
var wasClean = event.wasClean;
alert(code + "; " + reason + "; " + wasClean);
}
function onMessage(event) {
//do stuff
}
function onOpen(event) {
webSocket.send('opening socket');
}
function onError(event) {
alert(event.message);
}
对于我的servlet,我有
@ServerEndpoint("/test")
public class TranscoderSNSServlet extends HttpServlet {
...
}
我能找到的唯一有用的错误代码是event.code = 1006,event.wasClean = false。我使用的浏览器肯定支持websockets。我试图在ElasticBeanstalk上运行它。任何帮助表示赞赏