gpt4 book ai didi

java - Spring WebSocket(Stomp)客户端代理

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:25 24 4
gpt4 key购买 nike

我正在尝试使用 Spring 的 WebSocketStompClient。我需要设置代理才能访问 STOMP 服务器。我尝试使用通常的 socksProxySet、socksProxyHost、socksProxyPort 但没有成功。

你知道如何为 WebSocket(Stomp)Client 设置代理吗?

最佳答案

我知道这是一个相当古老的问题。尽管如此,我还是想给出一个新的答案,因为它可能对碰巧遇到相同问题的任何人有所帮助。

您可以提供自己的 RestTemplate,并为您的 stomp 客户端正确配置代理:

@Bean
public WebSocketStompClient client() {
StandardWebSocketClient client = new StandardWebSocketClient();
List<Transport> webSocketTransports = Arrays.asList(new WebSocketTransport(client), new RestTemplateXhrTransport(getRestTemplate()));
SockJsClient sockJsClient = new SockJsClient(webSocketTransports);
WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
stompClient.setAutoStartup(true);
stompClient.setMessageConverter(new MappingJackson2MessageConverter());
return stompClient;
}

// Rest template with proxy configuration
private RestTemplate getRestTemplate() {
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
if (environment.getProperty("http.proxySet", boolean.class, false)) {
String proxyHost = environment.getProperty("http.proxyHost");
String proxyUser = environment.getProperty("http.proxyUser");
String proxyPassword = environment.getProperty("http.proxyPassword");
Integer proxyPort = environment.getProperty("http.proxyPort", Integer.class);
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
new AuthScope(proxyHost, proxyPort),
new UsernamePasswordCredentials(proxyUser, proxyPassword));

HttpHost myProxy = new HttpHost(proxyHost, proxyPort);
clientBuilder.setProxy(myProxy)
.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
.setDefaultCredentialsProvider(credentialsProvider);
}

HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setHttpClient(clientBuilder.build());
return new RestTemplate(factory);
}

关于java - Spring WebSocket(Stomp)客户端代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36159859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com