Long story short:
Can't connect to websocket in spring boot when the URL is long.
I'm trying to connect to a Binance WebSocket endpoint. The URL format looks like this:
wss://stream.binance.com/stream?streams=symbol1@depth/symbol2depth
.
The most important part of the URL is the end, where I have to list each symbol and divide them by a /
: symbol1@depth/symbol2@depth/symbol3@depth...
I need to connect to around 300 symbols at once, which is why the URL gets long. When I try to connect, the WebSocket connection doesn't get established, and there are no logs or error messages.
However:
import jakarta.websocket.ContainerProvider;
import jakarta.websocket.WebSocketContainer;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.handler.TextWebSocketHandler;
public class WSDepthClient {
private final StandardWebSocketClient client;
public WSDepthClient(String url) {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
// set max buffer size to 10MB
container.setDefaultMaxTextMessageBufferSize(10 * 1024 * 1024);
client = new StandardWebSocketClient(container);
client.execute(new DepthHandler(), url);
}
private class DepthHandler extends TextWebSocketHandler {
@Override
public void afterConnectionEstablished(WebSocketSession session) {
// this is never printed when connecting to 300 symbols
System.out.println("Connection established successfully!");
}
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) {
System.out.println("Transport error: {}", exception.getMessage());
}
// other methods...
}
}
Please advice me what to do, thank you so much for your time!
According to the official binance documentation, requests support sending data in the request body, not in the url. Try doing it this way, it shouldn't depend on the url length.
As far as I understand this is still what you are trying to do, here is the binance documentation.