1313
1414public class BlockingJavaWebSocketClient extends WebSocketClient {
1515
16- private CountDownLatch connectLatch = new CountDownLatch (1 );
16+ private final CountDownLatch connectLatch = new CountDownLatch (1 );
1717 private volatile CountDownLatch responseLatch ;
1818 private final AtomicReference <String > responseMessage = new AtomicReference <>();
1919 private final AtomicReference <byte []> responseBytesMessage = new AtomicReference <>();
@@ -31,7 +31,7 @@ public void onOpen(ServerHandshake handshake) {
3131 }
3232
3333 public void onMessage (String message ) {
34- System .out .println ("收到消息: " + message );
34+ System .out .println ("收到消息: " + message );
3535 responseMessage .set (message );
3636 if (responseLatch != null ) {
3737 responseLatch .countDown ();
@@ -40,7 +40,7 @@ public void onMessage(String message) {
4040 }
4141
4242 public void onMessage (ByteBuffer byteBuffer ) {
43- System .out .println ("收到字节消息: " + byteBuffer );
43+ System .out .println ("收到字节消息: " + byteBuffer );
4444 responseBytesMessage .set (byteBuffer .array ());
4545 if (responseLatch != null ) {
4646 responseLatch .countDown ();
@@ -49,19 +49,17 @@ public void onMessage(ByteBuffer byteBuffer) {
4949 }
5050
5151 public void onClose (int code , String reason , boolean remote ) {
52- System .out .println ("连接关闭: " + code + " - " + reason );
52+ System .out .println ("连接关闭: " + code + " - " + reason );
5353 connected = false ;
54- // Signal any waiting threads
5554 if (responseLatch != null ) {
5655 responseLatch .countDown ();
5756 }
5857 connectLatch .countDown ();
5958 }
6059
6160 public void onError (Exception ex ) {
62- System .out .println ("连接错误: " + ex .getMessage ());
61+ System .out .println ("连接错误: " + ex .getMessage ());
6362 connected = false ;
64- // Signal any waiting threads
6563 if (responseLatch != null ) {
6664 responseLatch .countDown ();
6765 }
@@ -70,7 +68,6 @@ public void onError(Exception ex) {
7068 }
7169
7270 public String sendRequest (String message ) throws InterruptedException {
73- // Connect if not already connected
7471 if (!connected && !isOpen ()) {
7572 connect ();
7673 if (!connectLatch .await (5 , TimeUnit .SECONDS )) {
@@ -82,29 +79,19 @@ public String sendRequest(String message) throws InterruptedException {
8279 throw new IllegalStateException ("WebSocket connection is not open." );
8380 }
8481
85- // Reset response data and create new response latch for this request
8682 responseMessage .set (null );
8783 responseBytesMessage .set (null );
8884 responseLatch = new CountDownLatch (1 );
8985
90- // Send the message
9186 send (message );
92-
93- // Wait for response
9487 if (!responseLatch .await (10 , TimeUnit .SECONDS )) {
9588 throw new InterruptedException ("Timeout waiting for WebSocket response." );
9689 }
9790
98- // Check if connection was closed during wait
99- if (!connected ) {
100- throw new IllegalStateException ("WebSocket connection was closed while waiting for response." );
101- }
102-
10391 return responseMessage .get ();
10492 }
10593
10694 public byte [] sendRequest (ByteBuffer message ) throws InterruptedException {
107- // Connect if not already connected
10895 if (!connected && !isOpen ()) {
10996 connect ();
11097 if (!connectLatch .await (5 , TimeUnit .SECONDS )) {
@@ -116,34 +103,18 @@ public byte[] sendRequest(ByteBuffer message) throws InterruptedException {
116103 throw new IllegalStateException ("WebSocket connection is not open." );
117104 }
118105
119- // Reset response data and create new response latch for this request
120106 responseMessage .set (null );
121107 responseBytesMessage .set (null );
122108 responseLatch = new CountDownLatch (1 );
123109
124- // Send the message
125110 send (message );
126-
127- // Wait for response
128111 if (!responseLatch .await (10 , TimeUnit .SECONDS )) {
129112 throw new InterruptedException ("Timeout waiting for WebSocket response." );
130113 }
131114
132- // Check if connection was closed during wait
133- if (!connected ) {
134- throw new IllegalStateException ("WebSocket connection was closed while waiting for response." );
135- }
136-
137115 return responseBytesMessage .get ();
138116 }
139117
140- public void disconnect () {
141- if (connected && isOpen ()) {
142- close ();
143- }
144- }
145-
146-
147118 @ SneakyThrows
148119 public static String sendRequestWaitResponse (String entrypoint , String message ) {
149120 BlockingJavaWebSocketClient blockingJavaWebSocketClient = new BlockingJavaWebSocketClient (URI .create (entrypoint ));
0 commit comments