Reputation: 729
I have created Java websocket application using NetBeans IDE. And it is running on GlassFish server well. But when I change the server to Apache Tomcat then it is not runnig well. I can't create connection with client. Here is my client code (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);
}
this is my websocket endpoint code (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");
}
When I run on Tomcat server my output was 'Connection closed' but when I run on GlassFish server my output was 'Connection Established'.
I want to run my application on tomcat server. Help?
Upvotes: 4
Views: 1056