Home / Class/ WebSocketProtocolHandler Class — netty Architecture

WebSocketProtocolHandler Class — netty Architecture

Architecture documentation for the WebSocketProtocolHandler class in WebSocketProtocolHandler.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  d25f6f34_2ca3_7948_600a_8eae12cd9427["WebSocketProtocolHandler"]
  803203fd_2fc2_9c78_2f2e_03ec757859e4["WebSocketProtocolHandler.java"]
  d25f6f34_2ca3_7948_600a_8eae12cd9427 -->|defined in| 803203fd_2fc2_9c78_2f2e_03ec757859e4
  76bc0861_94e5_4564_8eac_db4ccd0bbbb6["WebSocketProtocolHandler()"]
  d25f6f34_2ca3_7948_600a_8eae12cd9427 -->|method| 76bc0861_94e5_4564_8eac_db4ccd0bbbb6
  9c13b7fd_3096_4d39_04cd_c64c31b5b4e2["decode()"]
  d25f6f34_2ca3_7948_600a_8eae12cd9427 -->|method| 9c13b7fd_3096_4d39_04cd_c64c31b5b4e2
  934234bb_e32f_a458_6c63_7f3fa59d8534["readIfNeeded()"]
  d25f6f34_2ca3_7948_600a_8eae12cd9427 -->|method| 934234bb_e32f_a458_6c63_7f3fa59d8534
  791f6a38_ba17_97d5_517e_667c306cfa1d["close()"]
  d25f6f34_2ca3_7948_600a_8eae12cd9427 -->|method| 791f6a38_ba17_97d5_517e_667c306cfa1d
  93dcb75c_433c_bb38_9442_952cb62c0d43["write()"]
  d25f6f34_2ca3_7948_600a_8eae12cd9427 -->|method| 93dcb75c_433c_bb38_9442_952cb62c0d43
  d3870783_fcf5_f1ea_0308_841e7da4d63f["closeSent()"]
  d25f6f34_2ca3_7948_600a_8eae12cd9427 -->|method| d3870783_fcf5_f1ea_0308_841e7da4d63f
  bab512bf_e3c9_2d9c_23a0_307fbdc7e956["applyCloseSentTimeout()"]
  d25f6f34_2ca3_7948_600a_8eae12cd9427 -->|method| bab512bf_e3c9_2d9c_23a0_307fbdc7e956
  9e0b4d67_283d_e138_f03c_597e02c8e943["WebSocketHandshakeException()"]
  d25f6f34_2ca3_7948_600a_8eae12cd9427 -->|method| 9e0b4d67_283d_e138_f03c_597e02c8e943
  fd8efeec_b40f_2a22_636c_571171ddbe28["bind()"]
  d25f6f34_2ca3_7948_600a_8eae12cd9427 -->|method| fd8efeec_b40f_2a22_636c_571171ddbe28
  ba1534a1_e103_39e9_486f_08f2c8815547["connect()"]
  d25f6f34_2ca3_7948_600a_8eae12cd9427 -->|method| ba1534a1_e103_39e9_486f_08f2c8815547
  ae43b205_80c4_9592_53d7_fdcbb015022e["disconnect()"]
  d25f6f34_2ca3_7948_600a_8eae12cd9427 -->|method| ae43b205_80c4_9592_53d7_fdcbb015022e
  0c3b557f_4320_35b3_ce83_18379e850b14["deregister()"]
  d25f6f34_2ca3_7948_600a_8eae12cd9427 -->|method| 0c3b557f_4320_35b3_ce83_18379e850b14
  5875625f_b7a4_a3aa_d8f0_6d12563faab3["read()"]
  d25f6f34_2ca3_7948_600a_8eae12cd9427 -->|method| 5875625f_b7a4_a3aa_d8f0_6d12563faab3

Relationship Graph

Source Code

codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketProtocolHandler.java lines 34–185

abstract class WebSocketProtocolHandler extends MessageToMessageDecoder<WebSocketFrame>
        implements ChannelOutboundHandler {

    private final boolean dropPongFrames;
    private final WebSocketCloseStatus closeStatus;
    private final long forceCloseTimeoutMillis;
    private ChannelPromise closeSent;

    /**
     * Creates a new {@link WebSocketProtocolHandler} that will <i>drop</i> {@link PongWebSocketFrame}s.
     */
    WebSocketProtocolHandler() {
        this(true);
    }

    /**
     * Creates a new {@link WebSocketProtocolHandler}, given a parameter that determines whether or not to drop {@link
     * PongWebSocketFrame}s.
     *
     * @param dropPongFrames
     *            {@code true} if {@link PongWebSocketFrame}s should be dropped
     */
    WebSocketProtocolHandler(boolean dropPongFrames) {
        this(dropPongFrames, null, 0L);
    }

    WebSocketProtocolHandler(boolean dropPongFrames,
                             WebSocketCloseStatus closeStatus,
                             long forceCloseTimeoutMillis) {
        super(WebSocketFrame.class);
        this.dropPongFrames = dropPongFrames;
        this.closeStatus = closeStatus;
        this.forceCloseTimeoutMillis = forceCloseTimeoutMillis;
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, List<Object> out) throws Exception {
        if (frame instanceof PingWebSocketFrame) {
            frame.content().retain();
            ctx.writeAndFlush(new PongWebSocketFrame(frame.content()));
            readIfNeeded(ctx);
            return;
        }
        if (frame instanceof PongWebSocketFrame && dropPongFrames) {
            readIfNeeded(ctx);
            return;
        }

        out.add(frame.retain());
    }

    private static void readIfNeeded(ChannelHandlerContext ctx) {
        if (!ctx.channel().config().isAutoRead()) {
            ctx.read();
        }
    }

    @Override
    public void close(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception {
        if (closeStatus == null || !ctx.channel().isActive()) {
            ctx.close(promise);
        } else {
            if (closeSent == null) {
                write(ctx, new CloseWebSocketFrame(closeStatus), ctx.newPromise());
            }
            flush(ctx);
            applyCloseSentTimeout(ctx);
            closeSent.addListener(future -> ctx.close(promise));
        }
    }

    @Override
    public void write(final ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        if (closeSent != null) {
            ReferenceCountUtil.release(msg);
            promise.setFailure(new ClosedChannelException());
        } else if (msg instanceof CloseWebSocketFrame) {
            closeSent(promise.unvoid());
            ctx.write(msg).addListener(new PromiseNotifier<Void, ChannelFuture>(false, closeSent));
        } else {
            ctx.write(msg, promise);

Frequently Asked Questions

What is the WebSocketProtocolHandler class?
WebSocketProtocolHandler is a class in the netty codebase, defined in codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketProtocolHandler.java.
Where is WebSocketProtocolHandler defined?
WebSocketProtocolHandler is defined in codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketProtocolHandler.java at line 34.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free