2016-07-26 2 views

답변

0

의 Netty는 PingWebSocketFrame 및 PongWebSocketFrame을받은 TextWebSocketFrame & BinaryWebSocketFrame 퐁/나는 클라이언트가 핑을 보낼 때 알고 싶은 PingWebSocketFramePongWebSocketFrame 처리하는 방법

channelRead0에서받을 수 있습니다 귀하의 SimpleChannelInboundHandler에서 처리 로직에 대한 추상화 WebSocketProtocolHandler을 참조하십시오. 그런

1

:

/** 
* * <strong>Please keep in mind that this method will be renamed to 
* {@code messageReceived(ChannelHandlerContext, I)} in 5.0.</strong> 
*/ 
@Override 
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { 
    Channel ch = ctx.channel(); 
    if (!handshaker.isHandshakeComplete()) { 
     handshaker.finishHandshake(ch, (FullHttpResponse) msg); 
     l.error("WebSocket Client connected!"); 
     handshakeFuture.setSuccess(); 
     return; 
    } 

    if (msg instanceof FullHttpResponse) { 
     FullHttpResponse response = (FullHttpResponse) msg; 
     throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" 
      + response.content().toString(CharsetUtil.UTF_8) + ')'); 
    } 


    WebSocketFrame frame = (WebSocketFrame) msg; 
    if (frame instanceof TextWebSocketFrame) { 
     TextWebSocketFrame textFrame = (TextWebSocketFrame) frame; 
     l.info("WebSocket Client received message:{} ", textFrame.text()); 

     //needed if the server close the socket if no ping send for long 
     //better to send the ping with a timer 
     // it allwos to choose the rate 
     ch.write(new PingWebSocketFrame()); 

    } else if (frame instanceof PongWebSocketFrame) { 
     l.info("WebSocket Client received pong"); 
    } else if (frame instanceof CloseWebSocketFrame) { 
     l.info("WebSocket Client received closing"); 
     ch.close(); 
    } 
} 
관련 문제