I used Tyrus to make the connection. Here is the code:
public void connect(String IP) {
WebSocketContainer webSocketContainer = ContainerProvider.getWebSocketContainer();
ClientEndpointConfig.Configurator configurator = new ClientEndpointConfig.Configurator() {
public void beforeRequest(Map<String, List<String>> headers) {
List<String> originValue = new ArrayList<>();
originValue.add("http://example.com");
headers.put("Origin", originValue);
}
};
ClientEndpointConfig clientConfig = ClientEndpointConfig.Builder.create()
.configurator(configurator)
.build();
try {
webSocketContainer.connectToServer(this, clientConfig, new URI(IP));
} catch (Exception e) {
...
}
}
My computer has a few IP addresses. I want to send a WebSocket request from selected IP - I want to bind the WebSocket to the specific local IP. Is it possible to do? Should I use another library?
EDIT: I found netty - it allows to set the local IP address manually.