fix: Jakarta WebSocket not work

This commit is contained in:
ReaJason
2025-05-28 01:22:54 +08:00
parent df6125c046
commit ba690268e7
6 changed files with 150 additions and 68 deletions
@@ -2,6 +2,7 @@ package com.reajason.javaweb.godzilla;
import lombok.SneakyThrows;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.framing.CloseFrame;
import org.java_websocket.handshake.ServerHandshake;
import java.net.URI;
@@ -13,7 +14,7 @@ import java.util.concurrent.atomic.AtomicReference;
public class BlockingJavaWebSocketClient extends WebSocketClient {
private CountDownLatch connectLatch = new CountDownLatch(1);
private CountDownLatch responseLatch = new CountDownLatch(1);
private volatile CountDownLatch responseLatch;
private final AtomicReference<String> responseMessage = new AtomicReference<>();
private final AtomicReference<byte[]> responseBytesMessage = new AtomicReference<>();
private volatile boolean connected = false;
@@ -24,38 +25,125 @@ public class BlockingJavaWebSocketClient extends WebSocketClient {
@Override
public void onOpen(ServerHandshake handshake) {
System.out.println("连接成功");
connected = true;
connectLatch.countDown();
}
@Override
public void onMessage(String message) {
System.out.println("收到消息: " + message);
responseMessage.set(message);
responseLatch.countDown();
if (responseLatch != null) {
responseLatch.countDown();
}
close();
}
@Override
public void onMessage(ByteBuffer byteBuffer) {
System.out.println("收到字节消息: " + byteBuffer);
responseBytesMessage.set(byteBuffer.array());
responseLatch.countDown();
if (responseLatch != null) {
responseLatch.countDown();
}
close();
}
@Override
public void onClose(int code, String reason, boolean remote) {
responseLatch.countDown();
connectLatch.countDown();
System.out.println("连接关闭: " + code + " - " + reason);
connected = false;
// Signal any waiting threads
if (responseLatch != null) {
responseLatch.countDown();
}
connectLatch.countDown();
}
@Override
public void onError(Exception ex) {
responseLatch.countDown();
connectLatch.countDown();
System.out.println("连接错误: " + ex.getMessage());
connected = false;
// Signal any waiting threads
if (responseLatch != null) {
responseLatch.countDown();
}
connectLatch.countDown();
ex.printStackTrace();
}
public String sendRequest(String message) throws InterruptedException {
// Connect if not already connected
if (!connected && !isOpen()) {
connect();
if (!connectLatch.await(5, TimeUnit.SECONDS)) {
throw new InterruptedException("Timeout during WebSocket connection.");
}
}
if (!connected || !isOpen()) {
throw new IllegalStateException("WebSocket connection is not open.");
}
// Reset response data and create new response latch for this request
responseMessage.set(null);
responseBytesMessage.set(null);
responseLatch = new CountDownLatch(1);
// Send the message
send(message);
// Wait for response
if (!responseLatch.await(10, TimeUnit.SECONDS)) {
throw new InterruptedException("Timeout waiting for WebSocket response.");
}
// Check if connection was closed during wait
if (!connected) {
throw new IllegalStateException("WebSocket connection was closed while waiting for response.");
}
return responseMessage.get();
}
public byte[] sendRequest(ByteBuffer message) throws InterruptedException {
// Connect if not already connected
if (!connected && !isOpen()) {
connect();
if (!connectLatch.await(5, TimeUnit.SECONDS)) {
throw new InterruptedException("Timeout during WebSocket connection.");
}
}
if (!connected || !isOpen()) {
throw new IllegalStateException("WebSocket connection is not open.");
}
// Reset response data and create new response latch for this request
responseMessage.set(null);
responseBytesMessage.set(null);
responseLatch = new CountDownLatch(1);
// Send the message
send(message);
// Wait for response
if (!responseLatch.await(10, TimeUnit.SECONDS)) {
throw new InterruptedException("Timeout waiting for WebSocket response.");
}
// Check if connection was closed during wait
if (!connected) {
throw new IllegalStateException("WebSocket connection was closed while waiting for response.");
}
return responseBytesMessage.get();
}
public void disconnect() {
if (connected && isOpen()) {
close();
}
}
@SneakyThrows
public static String sendRequestWaitResponse(String entrypoint, String message) {
BlockingJavaWebSocketClient blockingJavaWebSocketClient = new BlockingJavaWebSocketClient(URI.create(entrypoint));
@@ -68,46 +156,6 @@ public class BlockingJavaWebSocketClient extends WebSocketClient {
return blockingJavaWebSocketClient.sendRequest(message);
}
public String sendRequest(String message) throws InterruptedException {
connect();
if (!connectLatch.await(5, TimeUnit.SECONDS)) {
throw new InterruptedException("Timeout during WebSocket connection.");
}
if (!connected) {
throw new IllegalStateException("WebSocket connection is not open.");
}
responseMessage.set(null);
connectLatch = new CountDownLatch(1);
responseLatch = new CountDownLatch(1);
send(message);
if (!responseLatch.await(5, TimeUnit.SECONDS)) {
throw new InterruptedException("Timeout waiting for WebSocket response.");
}
return responseMessage.get();
}
public byte[] sendRequest(ByteBuffer message) throws InterruptedException {
connect();
if (!connectLatch.await(5, TimeUnit.SECONDS)) {
throw new InterruptedException("Timeout during WebSocket connection.");
}
if (!connected) {
throw new IllegalStateException("WebSocket connection is not open.");
}
responseBytesMessage.set(null);
connectLatch = new CountDownLatch(1);
responseLatch = new CountDownLatch(1);
send(message);
if (!responseLatch.await(5, TimeUnit.SECONDS)) {
throw new InterruptedException("Timeout waiting for WebSocket response.");
}
return responseBytesMessage.get();
}
public static void main(String[] args) {
String uri = "ws://localhost:8082/app/fuck";
System.out.println("Response 1: " + BlockingJavaWebSocketClient.sendRequestWaitResponse(uri, "id"));
@@ -231,9 +231,11 @@ public class GodzillaManager implements Closeable {
byte[] aes = aes(this.key, bytes, true);
String base64String = Base64.encodeBase64String(aes);
String response = BlockingJavaWebSocketClient.sendRequestWaitResponse(this.entrypoint, base64String);
byte[] x = aes(key, Base64.decodeBase64(response), false);
GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(x));
return "ok".equals(IOUtils.toString(gzipInputStream, StandardCharsets.UTF_8));
if(StringUtils.isNoneBlank(response)){
byte[] x = aes(key, Base64.decodeBase64(response), false);
GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(x));
return "ok".equals(IOUtils.toString(gzipInputStream, StandardCharsets.UTF_8));
}
}
return false;