From ed995b0d6625e00332c2e5ac1ec345b83a105e5f Mon Sep 17 00:00:00 2001 From: Dmitriy Dumanskiy Date: Sat, 2 Jul 2022 09:13:20 +0300 Subject: [PATCH] Replace ctx.channel().writeAndFlush with ctx.writeAndFlush in WebSockets handlers (#12540) Motivation: There is no actual need to call `ctx.channel().writeAndFlush` instead of `ctx.writeAndFlush`. Using the context is also more correct as it start at the right place in the pipeline. Modification: Use the context and not the channel Result: More correct code --- .../handler/codec/http/websocketx/WebSocketProtocolHandler.java | 2 +- .../websocketx/WebSocketServerProtocolHandshakeHandler.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketProtocolHandler.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketProtocolHandler.java index e7f87c0f9ee..ae7b4c54870 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketProtocolHandler.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketProtocolHandler.java @@ -69,7 +69,7 @@ abstract class WebSocketProtocolHandler extends MessageToMessageDecoder out) throws Exception { if (frame instanceof PingWebSocketFrame) { frame.content().retain(); - ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content())); + ctx.writeAndFlush(new PongWebSocketFrame(frame.content())); readIfNeeded(ctx); return; } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java index 2d658965255..5ddf6076547 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java @@ -142,7 +142,7 @@ private boolean checkNextUri(String uri, String websocketPath) { } private static void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) { - ChannelFuture f = ctx.channel().writeAndFlush(res); + ChannelFuture f = ctx.writeAndFlush(res); if (!isKeepAlive(req) || res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); }