I have a Java application that connects to a remote websocket server. As a client, I'm using the standard Java EE JSR356 WebSocket API:
javax.websocket.WebSocketContainer.connectToServer(...)
However, I haven't found a way to specify a connection timeout using this API. When I call the connectToServer(...) method, it block until it establishes a connection (which may never happen).
Is there a way to specify a connection timeout using the standard API? If not, are there any workarounds?
Unfortunately, this is not exposed by JSR 356 - WebSocket API for Java. You will need to use implementation feature, such as HANDSHAKE_TIMEOUT in Tyrus (reference implementation). Other implementations will most likely have something similar.
Seems like there is no ticket about this yet in WEBSOCKET_SPEC, so you can add one if you want (I was able to find only issue which is mentioning SSL properties - WEBSOCKET_SPEC-210).