ChannelInboundHandlerAdapter and SimpleChannelInboundHandler are both processor base classes provided by Netty.
The main differences between them:
ChannelInboundHandlerAdapter
ChannelInboundHandlerAdapter is a general processor base class inherited from ChannelInboundHandler and is used to process inbound messages (i.e. messages from the client to the server).
It does not do any specific message type limitations, so it is suitable for handling various types of inbound messages.
You can override the channelRead method to handle different types of messages, or override methods such as exceptionCaught to handle exceptions.
Features:
General: There is no specified message type, you can handle any type of inbound messages in the channelRead method.
Flexible: You need to manually determine the type of message and perform corresponding processing.
Applicable scenarios: It is suitable for handling multiple different types of messages, or when you want to process multiple types of messages.
public class WebSocketServerHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof TextWebSocketFrame) { // Process TextWebSocketFrame messages String request = ((TextWebSocketFrame) msg).text(); ().writeAndFlush(new TextWebSocketFrame("Hello, " + request)); } else { (ctx, msg); // Forward other messages } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { (); (); } }
SimpleChannelInboundHandler
SimpleChannelInboundHandler is a subclass of ChannelInboundHandlerAdapter, which is specifically used to handle inbound messages of a specific type. You need to specify the message type T (such as TextWebSocketFrame, ByteBuf, etc.) when creating SimpleChannelInboundHandler.
It automatically releases each message after processing it, thus avoiding the manual management of the life cycle of the message (such as calling).
If the message type does not match, it throws an exception, which makes it more suitable for handling a single type of message, simplifying type checking.
Features:
Generics: SimpleChannelInboundHandler Forces to specify a message type, which can avoid manual type checking and reduce the possibility of code errors.
Automatically release messages: After processing the message, SimpleChannelInboundHandler will automatically release messages, so you don't need to care about resource recycling.
Concise: Suitable for processing specific types of messages, the code is more concise and readable.
public class WebSocketServerHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> { @Override protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception { // Directly process TextWebSocketFrame messages, no type check is required String request = (); ().writeAndFlush(new TextWebSocketFrame("Hello, " + request)); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { (); (); } }
WebSocket Frame Type
WebSocket Frame (text frame)
Used to transfer text data, usually a UTF-8 encoded string.
This type of frame corresponds to the TextWebSocketFrame class.
TextWebSocketFrame is a single type of message framework used in Netty to represent text messages transmitted in WebSocket connections.
WebSocket Frame (binary frame)
Used to transfer binary data (such as files, pictures, videos, etc.).
Corresponding to the BinaryWebSocketFrame class.
This type of WebSocket frame is usually used to transfer binary data.
WebSocket Frame (Close Frame)
The frame used to close the WebSocket connection.
Corresponding to the CloseWebSocketFrame class.
Sending this type of frame can notify the other party to close the connection.
WebSocket Frame (Ping Frame)
Used to test whether the WebSocket connection is available (heartbeat).
Corresponding to the PingWebSocketFrame class.
It is generally sent by the client or server. After receiving the ping, the other party will usually send a Pong frame as a response.
WebSocket Frame (Pong frame)
Used to respond to ping frames.
Corresponding to the PongWebSocketFrame class.
The role of TextWebSocketFrame
TextWebSocketFrame is a WebSocket frame specially used to process text messages. It inherits the WebSocketFrame and encapsulates the text content. In actual WebSocket applications, clients and servers usually exchange information by sending and receiving TextWebSocketFrames, which are usually UTF-8 encoded text.
This is the end of this article about the commonly used processors of WebSocket based on Netty. For more related content on Netty WebSocket processors, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!