I am trying to establish a WebSocket connection using Spring StandardWebSocketClient, but getting an error due to proxy settings as the server is running behind the proxy. Below is the code I am using.
StandardWebSocketClient aStandardWebSocketClient=new StandardWebSocketClient();
WebSocketConnectionManager manager = new WebSocketConnectionManager(aStandardWebSocketClient, new WebSockethandler(), aWebSockURL);
Able to make a rest call by setting proxy configuration successfully.
Is there any proxy configuration available for making StandardWebSocketClient connect to websocket server?
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
InetSocketAddress address = new InetSocketAddress("proxyHost",8080);
Proxy proxy = new Proxy(Proxy.Type.HTTP,address);
factory.setProxy(proxy);
restTemplate.setRequestFactory(factory);
After Screening the sourcecode of the StandardWebSocketClient implementation I don't think that proxy settings are supported currently. However I was able to establish a wss
connection via a proxy using jetty
-libs in spring boot:
pom.xml
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-xml</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-api</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-client</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-common</artifactId>
<version>9.4.7.v20170914</version>
</dependency>
SpringBootJettyWSSclient.java
...
HttpClient httpClient = new HttpClient(new SslContextFactory());
// add proxy
if (this.proxyHost.trim().length() > 0) {
ProxyConfiguration proxyConfig = httpClient.getProxyConfiguration();
List<ProxyConfiguration.Proxy> proxies = proxyConfig.getProxies();
HttpProxy proxy = new HttpProxy(this.proxyHost, Integer.parseInt(this.proxyPort));
proxies.add(proxy);
}
// add proxy auth
if (this.proxyUser.trim().length() > 0) {
String fullProxy = "http://" + this.proxyUser + ":" + this.proxyPassword + "@" + this.proxyHost + ":" + this.proxyPort;
AuthenticationStore authenticationStore = httpClient.getAuthenticationStore();
URI uri = URI.create(fullProxy);
authenticationStore.addAuthentication(new BasicAuthentication(uri, Authentication.ANY_REALM, this.proxyUser, this.proxyPassword));
}
httpClient.start();
WebSocketClient client = new WebSocketClient(httpClient);
client.start();
client.connect(new MyListener(), new URI(this.wssURL));
socket.awaitClose(50, TimeUnit.SECONDS);
...