我使用Tyrus进行连接。这是代码:
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) {
...
}
}
我的电脑有几个IP地址。我想从选定的IP发送WebSocket请求 - 我想将WebSocket绑定到特定的本地IP。有可能吗?我应该使用另一个图书馆吗?
编辑:我发现netty - 它允许手动设置本地IP地址。
答案 0 :(得分:0)
本地地址将由我认为的操作系统选择 - 基于本地接口的路由表。
如果您有多个IP都可以到达目标主机,我会通过您要绑定的本地IP配置到该主机的(静态)路由。
答案 1 :(得分:0)
我使用代理而不是为请求选择IP。