TooTallNate / Java-WebSocket

A barebones WebSocket client and server implementation written in 100% Java.
http://tootallnate.github.io/Java-WebSocket
MIT License
10.47k stars 2.57k forks source link

How to properly close a connection from client? #1325

Closed svalaskevicius closed 1 year ago

svalaskevicius commented 1 year ago

Describe what you would like to know or do

Hi, I cannot find a proper way to close the connection fully from the client side:

Describe the solution you'd considered

Additional context

karan1908 commented 1 year ago

I closed the connection in following manner, but I don't think this is the grace full approach. private static final CountDownLatch responseLatch = new CountDownLatch(1);

`public static void main(String[] args) throws Exception { MYWebSocketClient myWebSocketClient = new MYWebSocketClient(new URI( "wss://.../"));

    myWebSocketClient.connectBlocking(30, TimeUnit.SECONDS);

    responseLatch.await(1, TimeUnit.SECONDS);

    myWebSocketClient.closeBlocking();
}`  
PhilipRoman commented 1 year ago

Yes, close() and closeBlocking() are the correct ways to close a socket. Are you getting any specific problems? I just checked, and no threads are left hanging.

svalaskevicius commented 1 year ago

hmm thanks! looks like it might have been a problem on my side - retrying a connection multiple times due to a threading issue..