Search in sources :

Example 16 with PingWebSocketFrame

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

the class WebSocketExtensionFilterTest method testAlwaysSkip.

@Test
public void testAlwaysSkip() {
    WebSocketExtensionFilter neverSkip = WebSocketExtensionFilter.ALWAYS_SKIP;
    BinaryWebSocketFrame binaryFrame = new BinaryWebSocketFrame();
    assertTrue(neverSkip.mustSkip(binaryFrame));
    assertTrue(binaryFrame.release());
    TextWebSocketFrame textFrame = new TextWebSocketFrame();
    assertTrue(neverSkip.mustSkip(textFrame));
    assertTrue(textFrame.release());
    PingWebSocketFrame pingFrame = new PingWebSocketFrame();
    assertTrue(neverSkip.mustSkip(pingFrame));
    assertTrue(pingFrame.release());
    PongWebSocketFrame pongFrame = new PongWebSocketFrame();
    assertTrue(neverSkip.mustSkip(pongFrame));
    assertTrue(pongFrame.release());
    CloseWebSocketFrame closeFrame = new CloseWebSocketFrame();
    assertTrue(neverSkip.mustSkip(closeFrame));
    assertTrue(closeFrame.release());
    ContinuationWebSocketFrame continuationFrame = new ContinuationWebSocketFrame();
    assertTrue(neverSkip.mustSkip(continuationFrame));
    assertTrue(continuationFrame.release());
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) Test(org.junit.jupiter.api.Test)

Example 17 with PingWebSocketFrame

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

the class WebSocketExtensionFilterTest method testNeverSkip.

@Test
public void testNeverSkip() {
    WebSocketExtensionFilter neverSkip = WebSocketExtensionFilter.NEVER_SKIP;
    BinaryWebSocketFrame binaryFrame = new BinaryWebSocketFrame();
    assertFalse(neverSkip.mustSkip(binaryFrame));
    assertTrue(binaryFrame.release());
    TextWebSocketFrame textFrame = new TextWebSocketFrame();
    assertFalse(neverSkip.mustSkip(textFrame));
    assertTrue(textFrame.release());
    PingWebSocketFrame pingFrame = new PingWebSocketFrame();
    assertFalse(neverSkip.mustSkip(pingFrame));
    assertTrue(pingFrame.release());
    PongWebSocketFrame pongFrame = new PongWebSocketFrame();
    assertFalse(neverSkip.mustSkip(pongFrame));
    assertTrue(pongFrame.release());
    CloseWebSocketFrame closeFrame = new CloseWebSocketFrame();
    assertFalse(neverSkip.mustSkip(closeFrame));
    assertTrue(closeFrame.release());
    ContinuationWebSocketFrame continuationFrame = new ContinuationWebSocketFrame();
    assertFalse(neverSkip.mustSkip(continuationFrame));
    assertTrue(continuationFrame.release());
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) Test(org.junit.jupiter.api.Test)

Example 18 with PingWebSocketFrame

use of io.netty.handler.codec.http.websocketx.PingWebSocketFrame in project autobahn-java by crossbario.

the class NettyWebSocketClientHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    Channel ch = ctx.channel();
    if (!mHandshaker.isHandshakeComplete()) {
        FullHttpResponse response = (FullHttpResponse) msg;
        String negotiatedSerializer = response.headers().get("Sec-WebSocket-Protocol");
        LOGGER.d(String.format("Negotiated serializer=%s", negotiatedSerializer));
        ISerializer serializer = initializeSerializer(negotiatedSerializer);
        mHandshaker.finishHandshake(ch, response);
        mHandshakeFuture.setSuccess();
        mTransportHandler.onConnect(mTransport, serializer);
    } else if (msg instanceof FullHttpResponse) {
        FullHttpResponse response = (FullHttpResponse) msg;
        throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
    } else if (msg instanceof BinaryWebSocketFrame) {
        BinaryWebSocketFrame binaryWebSocketFrame = (BinaryWebSocketFrame) msg;
        byte[] payload = new byte[binaryWebSocketFrame.content().readableBytes()];
        LOGGER.v(String.format("Received binary frame, content length=%s", payload.length));
        binaryWebSocketFrame.content().readBytes(payload);
        mTransportHandler.onMessage(payload, true);
    } else if (msg instanceof TextWebSocketFrame) {
        TextWebSocketFrame textWebSocketFrame = (TextWebSocketFrame) msg;
        byte[] payload = new byte[textWebSocketFrame.content().readableBytes()];
        LOGGER.v(String.format("Received Text frame, content length=%s", payload.length));
        textWebSocketFrame.content().readBytes(payload);
        mTransportHandler.onMessage(payload, false);
    } else if (msg instanceof PingWebSocketFrame) {
        PingWebSocketFrame pingWebSocketFrame = (PingWebSocketFrame) msg;
        ctx.writeAndFlush(new PongWebSocketFrame(pingWebSocketFrame.content().retain()));
    } else if (msg instanceof PongWebSocketFrame) {
        // Not really doing anything here.
        LOGGER.v("WebSocket Client received pong.");
    } else if (msg instanceof CloseWebSocketFrame) {
        CloseWebSocketFrame closeWebSocketFrame = (CloseWebSocketFrame) msg;
        LOGGER.d(String.format("Received Close frame, code=%s, reason=%s", closeWebSocketFrame.statusCode(), closeWebSocketFrame.reasonText()));
        close(ctx, closeWebSocketFrame.statusCode() == 1000, new CloseDetails(CloseDetails.REASON_DEFAULT, null));
    }
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) Channel(io.netty.channel.Channel) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) CloseDetails(io.crossbar.autobahn.wamp.types.CloseDetails) ISerializer(io.crossbar.autobahn.wamp.interfaces.ISerializer)

Example 19 with PingWebSocketFrame

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

the class WebsocketTest method testIssue663_3.

@Test
void testIssue663_3() throws Exception {
    AtomicBoolean incomingData = new AtomicBoolean();
    CountDownLatch latch = new CountDownLatch(1);
    disposableServer = createServer().handle((req, resp) -> resp.sendWebsocket((i, o) -> i.receiveFrames().then())).bindNow();
    createClient(disposableServer.port()).websocket().uri("/").handle((in, out) -> out.sendObject(Flux.just(new PingWebSocketFrame(), new CloseWebSocketFrame()).delayElements(Duration.ofMillis(100))).then(in.receiveFrames().doOnNext(f -> {
        if (f instanceof PongWebSocketFrame) {
            incomingData.set(true);
        }
    }).doOnComplete(latch::countDown).then())).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 20 with PingWebSocketFrame

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

the class WebsocketTest method testIssue663_4.

@Test
void testIssue663_4() throws Exception {
    AtomicBoolean incomingData = new AtomicBoolean();
    CountDownLatch latch = new CountDownLatch(1);
    disposableServer = createServer().handle((req, resp) -> resp.sendWebsocket((i, o) -> i.receiveFrames().then(), WebsocketServerSpec.builder().handlePing(true).build())).bindNow();
    createClient(disposableServer.port()).websocket().uri("/").handle((in, out) -> out.sendObject(Flux.just(new PingWebSocketFrame(), new CloseWebSocketFrame()).delayElements(Duration.ofMillis(100))).then(in.receiveFrames().doOnNext(f -> incomingData.set(true)).doOnComplete(latch::countDown).then())).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)

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