2017-12-04 4 views
0

Meine Anwendung ist hinter einem Unternehmens-Firewall läuft und ich brauche HTTP-Proxy (http://theclientproxy.net:8080) verwenden, um die Internet-Http-Proxy in Netty WebSocket-Client eine Verbindung zum Internet

Ich habe als unter dem Netty Client verwendet zu verbinden, https://github.com/netty/netty/tree/4.1/example/src/main/java/io/netty/example/http/websocketx/client

Code:

public final class WebSocketClient { 

     static final String URL = System.getProperty("url", "wss://127.0.0.1:8080/websocket"); 

     public static void main(String[] args) throws Exception { 
      URI uri = new URI(URL); 
      String scheme = uri.getScheme() == null? "ws" : uri.getScheme(); 
      final String host = uri.getHost() == null? "127.0.0.1" : uri.getHost(); 
      final int port; 
    final boolean ssl = "wss".equalsIgnoreCase(scheme); 
      final SslContext sslCtx; 
      if (ssl) { 
       sslCtx = SslContextBuilder.forClient() 
        .trustManager(InsecureTrustManagerFactory.INSTANCE).build(); 
      } else { 
       sslCtx = null; 
      } 


      EventLoopGroup group = new NioEventLoopGroup(); 
      try { 
     final WebSocketClientHandler handler = 
          new WebSocketClientHandler(
            WebSocketClientHandshakerFactory.newHandshaker(
              uri, WebSocketVersion.V13, null, true, new DefaultHttpHeaders())); 

        Bootstrap b = new Bootstrap(); 
        b.group(group) 
        .channel(NioSocketChannel.class) 
        .handler(new ChannelInitializer<SocketChannel>() { 
         @Override 
         protected void initChannel(SocketChannel ch) { 
          ChannelPipeline p = ch.pipeline(); 
          if (sslCtx != null) { 
           p.addFirst(new HttpProxyHandler(new InetSocketAddress("theclientproxy.net", 8080))); 
           p.addLast(sslCtx.newHandler(ch.alloc(), host, port)); 
          } 
          p.addLast(
            new HttpClientCodec(), 
            new HttpObjectAggregator(8192), 
            WebSocketClientCompressionHandler.INSTANCE, 
            handler); 
         } 
        }); 

        Channel ch = b.connect(uri.getHost(), port).sync().channel(); 
        handler.handshakeFuture().sync(); 

BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); 
      while (true) { 
       String msg = console.readLine(); //THIS IS NULL IN DATA CENTER LOGS 
       if (msg == null) { 
        break; 
       } else if ("bye".equals(msg.toLowerCase())) { 
        ch.writeAndFlush(new CloseWebSocketFrame()); 
        ch.closeFuture().sync(); 
        break; 
       } else if ("ping".equals(msg.toLowerCase())) { 
        WebSocketFrame frame = new PingWebSocketFrame(Unpooled.wrappedBuffer(new byte[] { 8, 1, 8, 1 })); 
        ch.writeAndFlush(frame); 
       } else { 
        WebSocketFrame frame = new TextWebSocketFrame(msg); 
        ch.writeAndFlush(frame); 
       } 
      } 
     } finally { 
      group.shutdownGracefully(); 
     } 

Handler:

public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object> { 

    private final WebSocketClientHandshaker handshaker; 
    private ChannelPromise handshakeFuture; 

    public WebSocketClientHandler(WebSocketClientHandshaker handshaker) { 
     this.handshaker = handshaker; 
    } 

    public ChannelFuture handshakeFuture() { 
     return handshakeFuture; 
    } 

    @Override 
    public void handlerAdded(ChannelHandlerContext ctx) { 
     handshakeFuture = ctx.newPromise(); 
    } 

    @Override 
    public void channelActive(ChannelHandlerContext ctx) { 
     handshaker.handshake(ctx.channel()); 
    } 

    @Override 
    public void channelInactive(ChannelHandlerContext ctx) { 
     System.out.println("WebSocket Client disconnected!"); 
    } 

    @Override 
    public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { 
     Channel ch = ctx.channel(); 
     if (!handshaker.isHandshakeComplete()) { 
      try { 
       handshaker.finishHandshake(ch, (FullHttpResponse) msg); 
       System.out.println("WebSocket Client connected!"); 
       handshakeFuture.setSuccess(); 
      } catch (WebSocketHandshakeException e) { 
       System.out.println("WebSocket Client failed to connect"); 
       handshakeFuture.setFailure(e); 
      } 
      return; 
     } 

Die Anwendung kann sich erfolgreich mit dem WebSocket-Serverendpunkt von meinem lokalen Computer aus verbinden.

Aber in der Firma Rechenzentrum, wo meine Anwendung eingesetzt wird, ich sehe, ist der msg Wert null und die websocket Client

getrennt

Ist, dass meine Verbindung bedeuten, bei Firewall blockiert? Wenn dies der Fall ist, warum hat die Anweisung "WebSocket Client!" wird überhaupt gedruckt?

Dank

+0

https://github.com/netty/netty/issues/5070 – firstpostcommenter

+0

https://stackoverflow.com/questions/45139443/netty-websocket-client-channel-always-gets-inactive-on-linux-server – firstpostcommenter

Antwort

Verwandte Themen