如何处理 Netty 4 中的 PING/PONG 帧?
How to handle PING/PONG frame in Netty 4?
class ClientWebSocketHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception {
}
}
我只能在 channelRead0
中收到 TextWebSocketFrame
& BinaryWebSocketFrame
如何处理PingWebSocketFrame
和PongWebSocketFrame
,我想知道客户端什么时候发送Ping/Pong
Netty 在任何 WebSocketFrame 到达您的 SimpleChannelInboundHandler.See 抽象 WebSocketProtocolHandler 处理逻辑之前已经收到 PingWebSocketFrame 和 PongWebSocketFrame。
像那样:
(知道 channelRead0 已被弃用,在 5.0 版本后应被删除或不受支持,如果您想要最新的答案,请在评论中提问)
/**
* * <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();
}
}
class ClientWebSocketHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception {
}
}
我只能在 channelRead0
TextWebSocketFrame
& BinaryWebSocketFrame
如何处理PingWebSocketFrame
和PongWebSocketFrame
,我想知道客户端什么时候发送Ping/Pong
Netty 在任何 WebSocketFrame 到达您的 SimpleChannelInboundHandler.See 抽象 WebSocketProtocolHandler 处理逻辑之前已经收到 PingWebSocketFrame 和 PongWebSocketFrame。
像那样: (知道 channelRead0 已被弃用,在 5.0 版本后应被删除或不受支持,如果您想要最新的答案,请在评论中提问)
/**
* * <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();
}
}