0

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.

Szymon Marczak
  • 1,055
  • 1
  • 16
  • 31

2 Answers2

0

The local address will be selected by the operating system I believe - based on the routing tables in place for your local interfaces.

If you had multiple IPs that could all reach the destination host I'd suhgest you configure a (static) route to that host through the local ip you want to bind.

Jan
  • 13,738
  • 3
  • 30
  • 55
  • By default the request is always sending by one IP. I want to set the IP myself. Is this possible? – Szymon Marczak Dec 27 '15 at 12:42
  • As i said - only if there's a route from that address to the server. – Jan Dec 27 '15 at 13:22
  • I think - there's another solution - http://stackoverflow.com/questions/30875927/tyrus-websockets-java-how-to-set-client-local-ip-address - but probably Tyrus doesn't support setting local IP address, and I will have to switch to another library... – Szymon Marczak Dec 27 '15 at 14:45
0

I used proxy instead of selecting IP for the request.

Szymon Marczak
  • 1,055
  • 1
  • 16
  • 31