2017-12-04 17 views
0

에 연결하기 위해 클라이언트를 웹 소켓과 나는 인터넷에 연결하기 위해 HTTP 프록시 (http://theclientproxy.net:8080)를 사용할 필요가 나는 등 아래의 Netty 클라이언트를 사용하고HTTP 프록시는 인터넷 내 응용 프로그램은 기업 방화벽 실행

, https://github.com/netty/netty/tree/4.1/example/src/main/java/io/netty/example/http/websocketx/client

코드 :

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(); 
     } 

처리기 :

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; 
     } 

응용 프로그램이 로컬 컴퓨터의 websocket 서버 끝점에 성공적으로 연결할 수 있습니다.

하지만 내 응용 프로그램이 배포 된 회사 데이터 센터의

, 나는 MSG의 값은 null입니다 참조하고 웹 소켓 클라이언트는 내 연결이 방화벽에서 차단 뜻

을 분리? 그렇다면 왜 "WebSocket Client connected!"라는 문장을 사용 했습니까? 전혀 인쇄되지 않습니까?

감사

+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

답변