简体   繁体   English

Spring WebSocket(Stomp)客户端代理

[英]Spring WebSocket(Stomp)Client proxy

I'm trying to use the WebSocketStompClient from Spring. 我正在尝试使用Spring的WebSocketStompClient I need to set a proxy to reach the STOMP server. 我需要设置一个代理来访问STOMP服务器。 I tried with the usual socksProxySet, socksProxyHost, socksProxyPort with no success. 我尝试使用通常的socksProxySet, socksProxyHost, socksProxyPort没有成功。

Do you have any idea how to set the proxy for the WebSocket(Stomp)Client ? 您是否知道如何为WebSocket(Stomp)Client设置代理?

I know this is a fairly old question. 我知道这是一个相当古老的问题。 Nonetheless, I'd like give a new answer as it may be of help for anyone that happen to stumble upon the same issue. 尽管如此,我想给出一个新的答案,因为它可能对碰巧遇到同样问题的人有所帮助。

You can provide your own RestTemplate with the proxy properly configured to your 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);
}

If it is Stomp-over-Websocket and if you have a native Stomp broker like RabbitMQ with Extension Plugin(s), you can use: 如果它是Stomp-over-Websocket,如果你有像RabbitMQ这样的本地Stomp代理和扩展插件,你可以使用:

https://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html#websocket-stomp-handle-broker-relay https://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html#websocket-stomp-handle-broker-relay

Note: The defaults don't like messages > 8kB 注意:默认值不喜欢> 8kB的消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM