如何为 Spring StandardWebSocketClient 设置代理

Gan*_*har 5 websocket spring-boot spring-websocket java-websocket

我正在尝试使用 Spring StandardWebSocketClient 建立 WebSocket 连接,但由于代理设置而出现错误,因为服务器在代理后面运行。下面是我正在使用的代码。

StandardWebSocketClient aStandardWebSocketClient=new StandardWebSocketClient();
WebSocketConnectionManager manager = new WebSocketConnectionManager(aStandardWebSocketClient, new WebSockethandler(), aWebSockURL);
Run Code Online (Sandbox Code Playgroud)

成功设置代理配置后可以进行休息呼叫。

是否有任何代理配置可用于使 StandardWebSocketClient 连接到 websocket 服务器?

SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
InetSocketAddress address = new InetSocketAddress("proxyHost",8080);
Proxy proxy = new Proxy(Proxy.Type.HTTP,address);
factory.setProxy(proxy);
restTemplate.setRequestFactory(factory);
Run Code Online (Sandbox Code Playgroud)

Ben*_*rth 0

在筛选 StandardWebSocketClient 实现的源代码后,我认为当前不支持代理设置。但是我能够在 Spring Boot 中wss使用 -libs 通过代理建立连接:jetty

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>
Run Code Online (Sandbox Code Playgroud)

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);
...
Run Code Online (Sandbox Code Playgroud)