SparkJava websocket not working

Viewed 848

SparkJava web sockets won't work. Whenever I attempt to connect to it with a websocket tester, at 'ws://localhost:4567/echo' it gets an error 'undefined' and never connects, nor do any of the sout's or printStackTrace get called.

@WebSocket
public class EchoWebSocket {
private static final Queue<Session> sessions = new ConcurrentLinkedQueue<>();

@OnWebSocketConnect
public void connected(Session session) {
    System.out.println("Client connected");

    //sessions.add(session);
}

@OnWebSocketClose
public void closed(Session session, int statusCode, String reason) {
    System.out.println("Client disconnected");

    //sessions.remove(session);
}

@OnWebSocketMessage
public void message(Session session, String message) throws IOException {
    System.out.println("Got: ");// + message);   // Print message

    //session.getRemote().sendString(message); // and send it back
}

@OnWebSocketError
public void throwError(Throwable error) {
    error.printStackTrace();
}
}

how I call it

Spark.webSocket("/echo", new EchoWebSocket());
Spark.init();
1 Answers

You need to define the class, not create an object.

Spark.webSocket("/echo", EchoWebSocket.class);
Related