2017-05-09 21 views
0

동일한 메시지를 에코 서버로 계속 보내고받은 바이트를 계산하기 위해 WebSocket 클라이언트를 구축했습니다. 단절과 같은 메시지를 반환합니다netty LoggingHandler는 한 프레임에 멀티 파일 메시지를 출력하고 너무 긴 것으로 계산합니다.

WebSocketData [수신 된 메시지]

을하지만 LoggingHandler 인쇄 로그를 검사 할 때, 수신 된 메시지는 함께 에코 서버 스틱 보냅니다. 그래서 LoggingHandler 앞에 BytesCountingHandler를 넣을 때 하나의 단일 메시지의 계산 결과가 평소보다 너무 큽니다.

내가 물어보고 싶은 것은 : 내가 잘못하고있는 것인가?

내 예비 클래스는 다음과 같습니다 :

연결하고 메시지를 보내

b.group(group) 
       .channel(NioSocketChannel.class) 
       .handler(new ChannelInitializer<SocketChannel>() { 
        @Override 
        protected void initChannel(SocketChannel ch) { 
         ChannelPipeline p = ch.pipeline(); 
         if (sslCtx != null) { 
          p.addLast(sslCtx.newHandler(ch.alloc(), host, port)); 
         } 
         p.addLast(
           new BytesCountHandler(), 
           new LoggingHandler(LogLevel.INFO), 
           new HttpClientCodec(), 
           new HttpObjectAggregator(8192), 
           handler); 
        } 
       }); 
     Channel ch = b.connect(uri.getHost(), port).sync().channel(); 
     handler.handshakeFuture().sync(); 

     for (;;) { 
      String msg = "hello websocket"; 
      WebSocketFrame frame = new TextWebSocketFrame(msg); 
      ch.writeAndFlush(frame); 
     } 

BytesCountHandler : LoggingHandler없이

public class BytesCountHandler extends ChannelDuplexHandler { 

@Override 
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { 
    super.write(ctx, msg, promise); 
} 

@Override 
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 
    System.out.println(countMessage(msg)); 
    super.channelRead(ctx, msg); 
} 

private int countMessage(Object msg) { 
    if (msg instanceof ByteBuf) { 
     return countBytes((ByteBuf) msg); 
    } else if (msg instanceof ByteBufHolder) { 
     return countByteBufHolder((ByteBufHolder) msg); 
    } 
    return 0; 
} 

private int countBytes(ByteBuf buf) { 
    return buf.readableBytes(); 
} 

private int countByteBufHolder(ByteBufHolder bufHolder) { 
    return countBytes(bufHolder.content()); 
} 

로그 :

129 
WebSocket Client connected! 
320 
96 
32 
32 
32 
192 
160 
320 
96 
448 
448 

을 LoggingHandler로 로그인 :

... 
WebSocket Client connected! 
五月 09, 2017 10:08:33 上午 io.netty.handler.logging.LoggingHandler logMessage 
信息: [id: 0xed597528, /10.128.7.104:20558 => /10.128.106.20:8085] WRITE(21B) 
     +-------------------------------------------------+ 
     | 0 1 2 3 4 5 6 7 8 9 a b c d e f | 
+--------+-------------------------------------------------+----------------+ 
|00000000| 81 8f 1f 79 b5 46 77 1c d9 2a 70 59 c2 23 7d 0a |...y.Fw..*pY.#}.| 
|00000010| da 25 74 1c c1         |.%t..   | 
+--------+-------------------------------------------------+----------------+ 
五月 09, 2017 10:08:33 上午 io.netty.handler.logging.LoggingHandler flush 
信息: [id: 0xed597528, /10.128.7.104:20558 => /10.128.106.20:8085] FLUSH 
五月 09, 2017 10:08:33 上午 io.netty.handler.logging.LoggingHandler logMessage 
信息: [id: 0xed597528, /10.128.7.104:20558 => /10.128.106.20:8085] RECEIVED(32B) 
     +-------------------------------------------------+ 
     | 0 1 2 3 4 5 6 7 8 9 a b c d e f | 
+--------+-------------------------------------------------+----------------+ 
|00000000| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[| 
|00000010| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]| 
+--------+-------------------------------------------------+----------------+ 
... 
五月 09, 2017 10:08:33 上午 io.netty.handler.logging.LoggingHandler logMessage 
信息: [id: 0xed597528, /10.128.7.104:20558 => /10.128.106.20:8085] RECEIVED(512B) 
     +-------------------------------------------------+ 
     | 0 1 2 3 4 5 6 7 8 9 a b c d e f | 
+--------+-------------------------------------------------+----------------+ 
|00000000| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[| 
|00000010| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]| 
|00000020| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[| 
|00000030| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]| 
|00000040| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[| 
|00000050| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]| 
|00000060| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[| 
|00000070| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]| 
|00000080| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[| 
|00000090| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]| 
|000000a0| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[| 
|000000b0| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]| 
|000000c0| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[| 
|000000d0| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]| 
|000000e0| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[| 
|000000f0| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]| 
|00000100| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[| 
|00000110| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]| 
|00000120| 81 1e 57 65 62 53 6f 63 6b 65 74 44 61 74 61 5b |..WebSocketData[| 
|00000130| 68 65 6c 6c 6f 20 77 65 62 73 6f 63 6b 65 74 5d |hello websocket]| 
... 

내가 분명히

감사

을 discribed 한 희망 당신은 낮은 수준의 네트워킹 데이터보다는 당신의 프레임의 내용을 계산하는

답변

0

. 대신 HTTP & WS 층으로 래퍼로, 계산과 웹 소켓 프레임의 내용을 인쇄 할 때문에

, 더 아래로 로깅 핸들러를 이동해야합니다!

p.addLast(        
    new HttpClientCodec(), 
    new HttpObjectAggregator(8192), 
    new LoggingHandler(LogLevel.INFO), 
    handler); 
+0

감사합니다 사실은 내가 계산합니다 HTTP 및 WS 계층에 의해 래퍼, 난 그냥 그런 식으로 작동하지 않았다 혼란. – ayw

+0

정확한 바이트 수를 세는 것은 정말로 어렵습니다. TCP가 모든 사실을 고려하여 들어오는 모든 버퍼를 추가하기 때문입니다. http://netty.io/wiki/user-guide-for-4.x.html#dealing -with-a-stream-based-transport 왜 이런 일이 일어나는가? – Ferrybig

+0

가져와, 고마워! – ayw