Search in sources :

Example 1 with ISerializer

use of io.crossbar.autobahn.wamp.interfaces.ISerializer 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

ISerializer (io.crossbar.autobahn.wamp.interfaces.ISerializer)1 CloseDetails (io.crossbar.autobahn.wamp.types.CloseDetails)1 Channel (io.netty.channel.Channel)1 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)1 CloseWebSocketFrame (io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)1 PingWebSocketFrame (io.netty.handler.codec.http.websocketx.PingWebSocketFrame)1 PongWebSocketFrame (io.netty.handler.codec.http.websocketx.PongWebSocketFrame)1 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)1