Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java websocket server giving 404

I'm trying to build a java websocket server. I've written a simple server endpoint:

@ServerEndpoint(value = "/test")
public class EndPoint {
    static Queue<Session> queue = new ConcurrentLinkedQueue<Session>();

    public static void send(int a, int b) {
        try {
            for(Session session : queue) {
                session.getBasicRemote().sendText("a = " + a + ",b=" + b);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    @OnOpen
    public void openConnection(Session session) {
        queue.add(session);
    }

    @OnClose
    public void closeConnection(Session session) {
        queue.remove(session);
    }

    @OnError
    public void error(Session session, Throwable t) {
        queue.remove(session);
    }
}

My web.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


</web-app>

When I hit ws://localhost:8080/websocket-1.0/test from Javascript, I get a 404 response. I'm using tomcat 8 to deploy this. What am I missing here?

like image 853
coder Avatar asked Nov 01 '22 23:11

coder


1 Answers

1) Use latest version of Tomcat 7.0.x or latest version of Tomcat 8.0.x

2) Make sure your war contains only the ServerEndpoint classes and an empty web.xml. Tomcat automatically scans and loads the classes annotated with ServerEndpoint annotation

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1"
  metadata-complete="true">

</web-app>

3) Verify that catalina.out has the following log msg and there are no exceptions.

org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /usr/local/tomcat/apache-tomcat-8.0.12/webapps/.war

like image 191
damodar Avatar answered Nov 15 '22 03:11

damodar