apache camel jetty websocket java.net.BindException

huangapple go评论99阅读模式
英文:

apache camel jetty websocket java.net.BindException

问题

以下是您提供的内容的中文翻译:

我尝试使用Apache Camel Jetty Websocket Component连接到一个WebSocket。
WebSocket的URL是:wss://echo.websocket.org

我遇到了以下错误:

  1. org.apache.camel.RuntimeCamelException: MultiException[java.io.IOException: Failed to bind to echo.websocket.org/174.129.224.73:443, java.io.IOException: Failed to bind to echo.websocket.org/174.129.224.73:443]

我还没有真正理解他是如何在ws和wss之间进行选择的,我尝试使用自签名的sslContextParameters。

以下是我的代码:

  1. @Configuration
  2. public class EipConfiguration extends RouteBuilder {
  3. @Override
  4. public void configure() {
  5. CamelContext context = new DefaultCamelContext();
  6. String server = "echo.websocket.org";
  7. int port = 443;
  8. String uri = "websocket://" + server + ":" + port + "/?sslContextParameters=#sslContextParameters";
  9. from(uri)
  10. .log(">>> Message received from WebSocket Client : ${body}");
  11. }
  12. @Bean(name = "sslContextParameters")
  13. public SSLContextParameters sslContextParameters() throws Exception {
  14. TrustManagersParameters trustManagersParameters = new TrustManagersParameters();
  15. X509ExtendedTrustManager extendedTrustManager = new InsecureX509TrustManager();
  16. trustManagersParameters.setTrustManager(extendedTrustManager);
  17. SSLContextParameters sslContextParameters = new SSLContextParameters();
  18. sslContextParameters.setTrustManagers(trustManagersParameters);
  19. return sslContextParameters;
  20. }
  21. }

InsecureX509TrustManager:

  1. public class InsecureX509TrustManager extends X509ExtendedTrustManager {
  2. @Override
  3. public void checkClientTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException {
  4. //Do nothing
  5. }
  6. @Override
  7. public void checkServerTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException {
  8. //Do nothing
  9. }
  10. @Override
  11. public void checkClientTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException {
  12. //Do nothing
  13. }
  14. @Override
  15. public void checkServerTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException {
  16. //Do nothing
  17. }
  18. @Override
  19. public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
  20. //Do nothing
  21. }
  22. @Override
  23. public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
  24. //Do nothing
  25. }
  26. @Override
  27. public X509Certificate[] getAcceptedIssuers() {
  28. return new X509Certificate[0];
  29. }
  30. }
英文:

I try to connect a websocket with Apache Camel Jetty Websocket Component
The Websocket's url is : wss://echo.websocket.org

I'am stuck with this error :

  1. org.apache.camel.RuntimeCamelException: MultiException[java.io.IOException: Failed to bind to echo.websocket.org/174.129.224.73:443, java.io.IOException: Failed to bind to echo.websocket.org/174.129.224.73:443]

I havent really understand how he choose between ws and wss , i Try to put a self signed sslContextParameters/

Here si my code :

  1. @Configuration
  2. public class EipConfiguration extends RouteBuilder {
  3. @Override
  4. public void configure() {
  5. CamelContext context = new DefaultCamelContext();
  6. String server = "echo.websocket.org";
  7. int port = 443;
  8. String uri = "websocket://"+server+":"+port+"/?sslContextParameters=#sslContextParameters";
  9. from(uri)
  10. .log(">>> Message received from WebSocket Client : ${body}");
  11. }
  12. @Bean(name = "sslContextParameters")
  13. public SSLContextParameters sslContextParameters() throws Exception {
  14. TrustManagersParameters trustManagersParameters = new TrustManagersParameters();
  15. X509ExtendedTrustManager extendedTrustManager = new InsecureX509TrustManager();
  16. trustManagersParameters.setTrustManager(extendedTrustManager);
  17. SSLContextParameters sslContextParameters = new SSLContextParameters();
  18. sslContextParameters.setTrustManagers(trustManagersParameters);
  19. return sslContextParameters;
  20. }
  21. }

InsecureX509TrustManager

  1. public class InsecureX509TrustManager extends X509ExtendedTrustManager {
  2. @Override
  3. public void checkClientTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException {
  4. //Do nothing
  5. }
  6. @Override
  7. public void checkServerTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException {
  8. //Do nothing
  9. }
  10. @Override
  11. public void checkClientTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException {
  12. //Do nothing
  13. }
  14. @Override
  15. public void checkServerTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException {
  16. //Do nothing
  17. }
  18. @Override
  19. public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
  20. //Do nothing
  21. }
  22. @Override
  23. public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
  24. //Do nothing
  25. }
  26. @Override
  27. public X509Certificate[] getAcceptedIssuers() {
  28. return new X509Certificate[0];
  29. }
  30. }

答案1

得分: 1

Jetty WebSocket组件(以及Atmosphere WebSocket组件)旨在公开新的WebSocket服务器。如果您需要作为客户端连接到远程WebSocket服务器,则应使用AHC WebSocket组件

> AHC-WS组件为客户端提供基于WebSocket的端点,用于与外部服务器通过WebSocket进行通信(作为客户端打开与外部服务器的WebSocket连接)。

英文:

Jetty WebSocket Component (and Atmosphere WebSocket component) is intended to expose new WebSocket server. If you need to connect as client to remote WebSocket server, you should use AHC Websocket component

> The AHC-WS component provides Websocket based endpoints for a client communicating with external servers over Websocket (as a client opening a websocket connection to an external server).

huangapple
  • 本文由 发表于 2020年7月26日 19:27:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63099494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定