Mar*_*ker 5 java websocket spark-java
SparkJava Web 套接字将无法工作。每当我尝试使用 websocket 测试器连接到它时,在“ws://localhost:4567/echo”处,它都会收到“未定义”错误,并且永远不会连接,也不会调用任何 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();
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么称呼它
Spark.webSocket("/echo", new EchoWebSocket());
Spark.init();
Run Code Online (Sandbox Code Playgroud)
小智 -1
您需要定义类,而不是创建对象。
Spark.webSocket("/echo", EchoWebSocket.class);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
935 次 |
| 最近记录: |