简体   繁体   中英

Does not connect to Java websocket in Tomcat

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?

Tomcat 6 did not have Websocket support. Upgrade to a higher version (at least Tomcat 7 with JDK 7).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM