SparkJava网络套接字无法正常工作。每当我尝试使用websocket测试仪连接到它时,请在' ws:// localhost:4567 / echo'它会出现错误' undefined'并且永远不会连接,也不会调用任何sout或printStackTrace。
@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();
}
}
我怎么称呼它
Spark.webSocket("/echo", new EchoWebSocket());
Spark.init();
答案 0 :(得分:-1)
您需要定义类,而不是创建对象。
Spark.webSocket("/echo", EchoWebSocket.class);