Search in sources :

Example 21 with WebSocketFrame

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

the class WebSocketClientHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    Channel ch = ctx.channel();
    if (!handshaker.isHandshakeComplete()) {
        try {
            handshaker.finishHandshake(ch, (FullHttpResponse) msg);
            System.out.println("WebSocket Client connected!");
            handshakeFuture.setSuccess();
        } catch (WebSocketHandshakeException e) {
            System.out.println("WebSocket Client failed to connect");
            handshakeFuture.setFailure(e);
        }
        return;
    }
    if (msg instanceof FullHttpResponse) {
        FullHttpResponse response = (FullHttpResponse) msg;
        throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
    }
    WebSocketFrame frame = (WebSocketFrame) msg;
    if (frame instanceof TextWebSocketFrame) {
        TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
        System.out.println("WebSocket Client received message: " + textFrame.text());
    } else if (frame instanceof PongWebSocketFrame) {
        System.out.println("WebSocket Client received pong");
    } else if (frame instanceof CloseWebSocketFrame) {
        System.out.println("WebSocket Client received closing");
        ch.close();
    }
}
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) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) WebSocketHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketHandshakeException)

Example 22 with WebSocketFrame

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

the class DeflateEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception {
    final ByteBuf compressedContent;
    if (msg.content().isReadable()) {
        compressedContent = compressContent(ctx, msg);
    } else if (msg.isFinalFragment()) {
        // Set empty DEFLATE block manually for unknown buffer size
        // https://tools.ietf.org/html/rfc7692#section-7.2.3.6
        compressedContent = EMPTY_DEFLATE_BLOCK.duplicate();
    } else {
        throw new CodecException("cannot compress content buffer");
    }
    final WebSocketFrame outMsg;
    if (msg instanceof TextWebSocketFrame) {
        outMsg = new TextWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
    } else if (msg instanceof BinaryWebSocketFrame) {
        outMsg = new BinaryWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
    } else if (msg instanceof ContinuationWebSocketFrame) {
        outMsg = new ContinuationWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
    } else {
        throw new CodecException("unexpected frame type: " + msg.getClass().getName());
    }
    out.add(outMsg);
}
Also used : ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) CodecException(io.netty.handler.codec.CodecException) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 23 with WebSocketFrame

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

the class NettyWebSocket method send.

@Override
public void send(byte[] payload, boolean isBinary) {
    WebSocketFrame frame;
    if (isBinary) {
        frame = new BinaryWebSocketFrame(toByteBuf(payload));
    } else {
        frame = new TextWebSocketFrame(toByteBuf(payload));
    }
    mChannel.writeAndFlush(frame);
}
Also used : TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame)

Example 24 with WebSocketFrame

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

the class WebsocketTest method testCloseWebSocketFrameSentByClient.

@Test
void testCloseWebSocketFrameSentByClient() {
    disposableServer = createServer().handle((req, res) -> res.sendWebsocket((in, out) -> out.sendString(Mono.just("echo")).sendObject(new CloseWebSocketFrame()))).bindNow();
    Mono<Void> response = createClient(disposableServer.port()).websocket().uri("/").handle((in, out) -> out.sendObject(in.receiveFrames().doOnNext(WebSocketFrame::retain).then())).next();
    StepVerifier.create(response).expectComplete().verify(Duration.ofSeconds(30));
}
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) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BaseHttpTest(reactor.netty.BaseHttpTest) Test(org.junit.jupiter.api.Test)

Example 25 with WebSocketFrame

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

the class WebsocketTest method testIssue900_2.

@Test
void testIssue900_2() throws Exception {
    AtomicReference<WebSocketCloseStatus> statusServer = new AtomicReference<>();
    AtomicReference<String> incomingData = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    disposableServer = createServer().handle((req, res) -> res.sendWebsocket((in, out) -> {
        in.receiveCloseStatus().doOnNext(o -> {
            statusServer.set(o);
            latch.countDown();
        }).subscribe();
        return out.sendObject(Flux.just(new TextWebSocketFrame("echo"), new CloseWebSocketFrame(1008, "something")).delayElements(Duration.ofMillis(100))).then(in.receiveFrames().doOnNext(o -> {
            if (o instanceof TextWebSocketFrame) {
                incomingData.set(((TextWebSocketFrame) o).text());
            }
        }).then());
    })).bindNow();
    createClient(disposableServer.port()).websocket().uri("/").handle((in, out) -> out.sendObject(in.receiveFrames().doOnNext(WebSocketFrame::retain))).subscribe();
    assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
    assertThat(incomingData.get()).isNotNull().isEqualTo("echo");
    assertThat(statusServer.get()).isNotNull().isEqualTo(new WebSocketCloseStatus(1008, "something"));
}
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) WebSocketCloseStatus(io.netty.handler.codec.http.websocketx.WebSocketCloseStatus) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) BaseHttpTest(reactor.netty.BaseHttpTest) Test(org.junit.jupiter.api.Test)

Aggregations

WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)25 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)21 CloseWebSocketFrame (io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)12 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)11 ByteBuf (io.netty.buffer.ByteBuf)9 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)9 Test (org.junit.jupiter.api.Test)9 ContinuationWebSocketFrame (io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame)8 PingWebSocketFrame (io.netty.handler.codec.http.websocketx.PingWebSocketFrame)6 PongWebSocketFrame (io.netty.handler.codec.http.websocketx.PongWebSocketFrame)6 Channel (io.netty.channel.Channel)4 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)4 WebSocketHandshakeException (io.netty.handler.codec.http.websocketx.WebSocketHandshakeException)4 URI (java.net.URI)4 Unpooled (io.netty.buffer.Unpooled)3 CorruptedFrameException (io.netty.handler.codec.CorruptedFrameException)3 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)3 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)3 ClientCookieDecoder (io.netty.handler.codec.http.cookie.ClientCookieDecoder)3 ClientCookieEncoder (io.netty.handler.codec.http.cookie.ClientCookieEncoder)3