Search in sources :

Example 51 with BinaryWebSocketFrame

use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame 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)

Aggregations

BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)51 ByteBuf (io.netty.buffer.ByteBuf)24 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)24 Test (org.junit.jupiter.api.Test)23 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)19 ContinuationWebSocketFrame (io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame)17 WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)11 FrameChecker (io.undertow.websockets.utils.FrameChecker)8 WebSocketTestClient (io.undertow.websockets.utils.WebSocketTestClient)8 URI (java.net.URI)8 FutureResult (org.xnio.FutureResult)8 CloseWebSocketFrame (io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 PingWebSocketFrame (io.netty.handler.codec.http.websocketx.PingWebSocketFrame)6 PongWebSocketFrame (io.netty.handler.codec.http.websocketx.PongWebSocketFrame)6 ServerWebSocketContainer (io.undertow.websockets.jsr.ServerWebSocketContainer)6 UndertowSession (io.undertow.websockets.jsr.UndertowSession)6 AnnotatedClientEndpoint (io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Endpoint (javax.websocket.Endpoint)6