我已经使用NetBeans IDE创建了Java websocket应用程序。它在GlassFish服务器上运行良好。但是,当我将服务器更改为ApacheTomcat时,它就不能很好地运行。我无法创建与客户端的连接。这是我的客户端代码(JavaScript)
if(webSocket !== undefined && webSocket.readyState !== WebSocket.CLOSED){
writeResponse("WebSocket is already opened.");
return;
}
webSocket = new WebSocket("ws://localhost:8080/Sl2World/slworldendpoint");
webSocket.onopen = function(event){
if(event.data === undefined)
return;
writeResponse(event.data);
};
webSocket.onmessage = function(event){
writeResponse(event.data);
};
webSocket.onclose = function(event){
writeResponse("Connection closed");
};
function writeResponse(text){
alert(text);
}
这是我的websocket端点代码(java)
@OnOpen
public void onOpen(Session session){
try {
session.getBasicRemote().sendText("Connection Established");
} catch (IOException ex) {
ex.printStackTrace();
}
}
@OnMessage
public void onMessage(String message, Session session){
System.out.println("Message from " + session.getId() + ": " + message);
try {
sess.getBasicRemote().sendText("message send");
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
@OnClose
public void onClose(Session session){
System.out.println("Session " +session.getId()+" has ended");
}
当我在Tomcat服务器上运行时,我的输出是"连接关闭"但当我在GlassFish服务器上运行时,我的输出是"Connection Established"。
我想在tomcat服务器上运行我的应用程序。帮助
Tomcat 6不支持Websocket。升级至更高版本(至少Tomcat 7和JDK 7)。