Search in sources :

Example 41 with CloseWebSocketFrame

use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project reactor-netty by reactor.

the class WebsocketServerOperations method onInboundNext.

@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void onInboundNext(ChannelHandlerContext ctx, Object frame) {
    if (frame instanceof CloseWebSocketFrame && ((CloseWebSocketFrame) frame).isFinalFragment()) {
        if (log.isDebugEnabled()) {
            log.debug(format(channel(), "CloseWebSocketFrame detected. Closing Websocket"));
        }
        CloseWebSocketFrame closeFrame = new CloseWebSocketFrame(true, ((CloseWebSocketFrame) frame).rsv(), ((CloseWebSocketFrame) frame).content());
        if (closeFrame.statusCode() != -1) {
            // terminate() will invoke onInboundComplete()
            sendCloseNow(closeFrame, f -> terminate());
        } else {
            // terminate() will invoke onInboundComplete()
            sendCloseNow(closeFrame, WebSocketCloseStatus.EMPTY, f -> terminate());
        }
        return;
    }
    if (!this.proxyPing && frame instanceof PingWebSocketFrame) {
        // "FutureReturnValueIgnored" this is deliberate
        ctx.writeAndFlush(new PongWebSocketFrame(((PingWebSocketFrame) frame).content()));
        ctx.read();
        return;
    }
    if (frame != LastHttpContent.EMPTY_LAST_CONTENT) {
        super.onInboundNext(ctx, frame);
    }
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame)

Aggregations

CloseWebSocketFrame (io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)41 PingWebSocketFrame (io.netty.handler.codec.http.websocketx.PingWebSocketFrame)23 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)23 PongWebSocketFrame (io.netty.handler.codec.http.websocketx.PongWebSocketFrame)22 WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)17 URI (java.net.URI)13 CountDownLatch (java.util.concurrent.CountDownLatch)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 WebSocketHandshakeException (io.netty.handler.codec.http.websocketx.WebSocketHandshakeException)11 Test (org.junit.jupiter.api.Test)11 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)10 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)10 WebSocketClientHandshakeException (io.netty.handler.codec.http.websocketx.WebSocketClientHandshakeException)10 TimeUnit (java.util.concurrent.TimeUnit)10 Consumer (java.util.function.Consumer)10 Function (java.util.function.Function)10 Unpooled (io.netty.buffer.Unpooled)9 CorruptedFrameException (io.netty.handler.codec.CorruptedFrameException)9