Search in sources :

Example 6 with PingWebSocketFrame

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

the class WebsocketClientOperations method onInboundNext.

@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void onInboundNext(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof FullHttpResponse) {
        started = true;
        channel().pipeline().remove(HttpObjectAggregator.class);
        FullHttpResponse response = (FullHttpResponse) msg;
        setNettyResponse(response);
        if (notRedirected(response)) {
            try {
                handshaker.finishHandshake(channel(), response);
                // This change is needed after the Netty change https://github.com/netty/netty/pull/11966
                ctx.read();
                listener().onStateChange(this, HttpClientState.RESPONSE_RECEIVED);
            } catch (Exception e) {
                onInboundError(e);
                // "FutureReturnValueIgnored" this is deliberate
                ctx.close();
            } finally {
                // Release unused content (101 status)
                response.content().release();
            }
        } else {
            response.content().release();
            listener().onUncaughtException(this, redirecting);
        }
        return;
    }
    if (!this.proxyPing && msg instanceof PingWebSocketFrame) {
        // "FutureReturnValueIgnored" this is deliberate
        ctx.writeAndFlush(new PongWebSocketFrame(((PingWebSocketFrame) msg).content()));
        ctx.read();
        return;
    }
    if (msg instanceof CloseWebSocketFrame && ((CloseWebSocketFrame) msg).isFinalFragment()) {
        if (log.isDebugEnabled()) {
            log.debug(format(channel(), "CloseWebSocketFrame detected. Closing Websocket"));
        }
        CloseWebSocketFrame closeFrame = new CloseWebSocketFrame(true, ((CloseWebSocketFrame) msg).rsv(), ((CloseWebSocketFrame) msg).content());
        if (closeFrame.statusCode() != -1) {
            sendCloseNow(closeFrame);
        } else {
            sendCloseNow(closeFrame, WebSocketCloseStatus.EMPTY);
        }
        onInboundComplete();
    } else if (msg != LastHttpContent.EMPTY_LAST_CONTENT) {
        super.onInboundNext(ctx, msg);
    }
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) WebSocketClientHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketClientHandshakeException)

Example 7 with PingWebSocketFrame

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

the class WebsocketTest method testIssue663_1.

@Test
void testIssue663_1() throws Exception {
    AtomicBoolean incomingData = new AtomicBoolean();
    CountDownLatch latch = new CountDownLatch(1);
    disposableServer = createServer().handle((req, resp) -> resp.sendWebsocket((i, o) -> o.sendObject(Flux.just(new PingWebSocketFrame(), new CloseWebSocketFrame()).delayElements(Duration.ofMillis(100))).then(i.receiveFrames().doOnNext(f -> {
        if (f instanceof PongWebSocketFrame) {
            incomingData.set(true);
        }
    }).doOnComplete(latch::countDown).then()))).bindNow();
    createClient(disposableServer.port()).websocket().uri("/").handle((in, out) -> in.receiveFrames()).subscribe();
    assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
    assertThat(incomingData.get()).isTrue();
}
Also used : StepVerifier(reactor.test.StepVerifier) AbortedException(reactor.netty.channel.AbortedException) BiFunction(java.util.function.BiFunction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BaseHttpTest(reactor.netty.BaseHttpTest) Unpooled(io.netty.buffer.Unpooled) WebsocketOutbound(reactor.netty.http.websocket.WebsocketOutbound) ConnectionObserver(reactor.netty.ConnectionObserver) Loggers(reactor.util.Loggers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Logger(reactor.util.Logger) URI(java.net.URI) Predicate(java.util.function.Predicate) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) WebSocketClientHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketClientHandshakeException) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketCloseStatus(io.netty.handler.codec.http.websocketx.WebSocketCloseStatus) Tuple2(reactor.util.function.Tuple2) WebSocketHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketHandshakeException) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Nullable(reactor.util.annotation.Nullable) ArrayList(java.util.ArrayList) WebsocketServerSpec(reactor.netty.http.server.WebsocketServerSpec) Charset(java.nio.charset.Charset) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ClientCookieEncoder(io.netty.handler.codec.http.cookie.ClientCookieEncoder) Schedulers(reactor.core.scheduler.Schedulers) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) Connection(reactor.netty.Connection) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) WebsocketInbound(reactor.netty.http.websocket.WebsocketInbound) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) ConnectionProvider(reactor.netty.resources.ConnectionProvider) CorruptedFrameException(io.netty.handler.codec.CorruptedFrameException) ClientCookieDecoder(io.netty.handler.codec.http.cookie.ClientCookieDecoder) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) CountDownLatch(java.util.concurrent.CountDownLatch) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) BaseHttpTest(reactor.netty.BaseHttpTest) Test(org.junit.jupiter.api.Test)

Example 8 with PingWebSocketFrame

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

the class WebsocketTest method testIssue663_2.

@Test
void testIssue663_2() throws Exception {
    AtomicBoolean incomingData = new AtomicBoolean();
    CountDownLatch latch = new CountDownLatch(1);
    disposableServer = createServer().handle((req, resp) -> resp.sendWebsocket((i, o) -> o.sendObject(Flux.just(new PingWebSocketFrame(), new CloseWebSocketFrame()).delayElements(Duration.ofMillis(100))).then(i.receiveFrames().doOnNext(f -> incomingData.set(true)).doOnComplete(latch::countDown).then()))).bindNow();
    createClient(disposableServer.port()).websocket(WebsocketClientSpec.builder().handlePing(true).build()).uri("/").handle((in, out) -> in.receiveFrames()).subscribe();
    assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
    assertThat(incomingData.get()).isFalse();
}
Also used : StepVerifier(reactor.test.StepVerifier) AbortedException(reactor.netty.channel.AbortedException) BiFunction(java.util.function.BiFunction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BaseHttpTest(reactor.netty.BaseHttpTest) Unpooled(io.netty.buffer.Unpooled) WebsocketOutbound(reactor.netty.http.websocket.WebsocketOutbound) ConnectionObserver(reactor.netty.ConnectionObserver) Loggers(reactor.util.Loggers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Logger(reactor.util.Logger) URI(java.net.URI) Predicate(java.util.function.Predicate) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) WebSocketClientHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketClientHandshakeException) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketCloseStatus(io.netty.handler.codec.http.websocketx.WebSocketCloseStatus) Tuple2(reactor.util.function.Tuple2) WebSocketHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketHandshakeException) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Nullable(reactor.util.annotation.Nullable) ArrayList(java.util.ArrayList) WebsocketServerSpec(reactor.netty.http.server.WebsocketServerSpec) Charset(java.nio.charset.Charset) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ClientCookieEncoder(io.netty.handler.codec.http.cookie.ClientCookieEncoder) Schedulers(reactor.core.scheduler.Schedulers) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) Connection(reactor.netty.Connection) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) WebsocketInbound(reactor.netty.http.websocket.WebsocketInbound) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) ConnectionProvider(reactor.netty.resources.ConnectionProvider) CorruptedFrameException(io.netty.handler.codec.CorruptedFrameException) ClientCookieDecoder(io.netty.handler.codec.http.cookie.ClientCookieDecoder) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) BaseHttpTest(reactor.netty.BaseHttpTest) Test(org.junit.jupiter.api.Test)

Example 9 with PingWebSocketFrame

use of io.netty.handler.codec.http.websocketx.PingWebSocketFrame in project async-http-client by AsyncHttpClient.

the class NettyWebSocket method sendPing.

@Override
public WebSocket sendPing(final byte[] payload, final WebSocketWriteCompleteListener listener) {
    final ChannelPromise channelPromise = channel.newPromise();
    channelPromise.addListener(listener);
    channel.writeAndFlush(new PingWebSocketFrame(wrappedBuffer(payload)), channelPromise);
    return this;
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame)

Example 10 with PingWebSocketFrame

use of io.netty.handler.codec.http.websocketx.PingWebSocketFrame in project async-http-client by AsyncHttpClient.

the class NettyWebSocket method handleFrame.

public void handleFrame(WebSocketFrame frame) {
    if (frame instanceof TextWebSocketFrame) {
        onTextFrame((TextWebSocketFrame) frame);
    } else if (frame instanceof BinaryWebSocketFrame) {
        onBinaryFrame((BinaryWebSocketFrame) frame);
    } else if (frame instanceof CloseWebSocketFrame) {
        Channels.setDiscard(channel);
        CloseWebSocketFrame closeFrame = (CloseWebSocketFrame) frame;
        onClose(closeFrame.statusCode(), closeFrame.reasonText());
        Channels.silentlyCloseChannel(channel);
    } else if (frame instanceof PingWebSocketFrame) {
        onPing((PingWebSocketFrame) frame);
    } else if (frame instanceof PongWebSocketFrame) {
        onPong((PongWebSocketFrame) frame);
    }
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame)

Aggregations

PingWebSocketFrame (io.netty.handler.codec.http.websocketx.PingWebSocketFrame)21 CloseWebSocketFrame (io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)18 PongWebSocketFrame (io.netty.handler.codec.http.websocketx.PongWebSocketFrame)17 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)14 WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)7 URI (java.net.URI)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Test (org.junit.jupiter.api.Test)6 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)5 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)5 WebSocketClientHandshakeException (io.netty.handler.codec.http.websocketx.WebSocketClientHandshakeException)5 WebSocketHandshakeException (io.netty.handler.codec.http.websocketx.WebSocketHandshakeException)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 Unpooled (io.netty.buffer.Unpooled)4 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 CorruptedFrameException (io.netty.handler.codec.CorruptedFrameException)4 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)4 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)4 ClientCookieDecoder (io.netty.handler.codec.http.cookie.ClientCookieDecoder)4 ClientCookieEncoder (io.netty.handler.codec.http.cookie.ClientCookieEncoder)4