TooTallNate / Java-WebSocket

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

How to make the connect time out parameter work when connect socket #1194

Closed 4Mi closed 5 months ago

4Mi commented 3 years ago

Describe what you would like to know or do Precondition:

  1. the network signal is not good.
  2. the url like "wss://..."

when i create socket with a connect timeout value 5 seconds as following: super(serverUri, new Draft_6455(), null, 5000);

after calling sockect.connect(), the onOpen() is not called, but after 15 minutes, the onError is called with following stack: 11-09 13:44:41.378 D/MssSocket( 6934): onError e: javax.net.ssl.SSLHandshakeException: SSL handshake aborted: ssl=0x8f209c80: I/O error during system call, Connection timed out 11-09 13:44:41.378 D/MssSocket( 6934): at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method) 11-09 13:44:41.378 D/MssSocket( 6934): at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:324) 11-09 13:44:41.378 D/MssSocket( 6934): at com.android.org.conscrypt.OpenSSLSocketImpl.waitForHandshake(OpenSSLSocketImpl.java:629) 11-09 13:44:41.378 D/MssSocket( 6934): at com.android.org.conscrypt.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:591) 11-09 13:44:41.378 D/MssSocket( 6934): at org.java_websocket.client.WebSocketClient.run(WebSocketClient.java:407)

It seems like that the connect timeout value which is set by constructor does not work.

Also, during the 15 minutes, i try closing the opening socket, but onClose() is not called. How can I set the connect timeout correctly? THANKS

marci4 commented 3 years ago

Hey,

looking at the code I would say that the timeout is set and used.

Best regards, Marcel

sirhcel commented 1 year ago

Could it be the case that the timeout is "just" set at the socket but the connectLatch which gets awaited in connectBlocking does not get notified?

I'm experiencing the problem when the service I'm trying to connecting to is down. The application sets a timeout via the JSON configuration but WebSocketClient.connectBlocking does not return. Shouldn't a timeout from the socket connection also notify connectLatch so that connectBlocking() can actually return as advertised?

Using connectBlocking(long, TimeUnit) with the same timeout instead of connectBlocking() returns and allows my application to retry.

srehlig commented 5 months ago

Hi @marci4, I think @sirhcel's argumentation is quite robust and seems to be a solution to the problem. Could you share some insights on why you resolved this issue as "not planned"?