Search in sources :

Example 16 with WebSocketFrame

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

the class WebSocketClientHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    Channel ch = ctx.channel();
    if (!handshaker.isHandshakeComplete()) {
        handshaker.finishHandshake(ch, (FullHttpResponse) msg);
        LOGGER.debug("WebSocket Client connected!");
        handshakeFuture.setSuccess();
        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;
        LOGGER.debug("WebSocket Client received text message: " + textFrame.text());
        String textReceived = textFrame.text();
        callback.call(textReceived);
    } else if (frame instanceof CloseWebSocketFrame) {
        isConnected = false;
        LOGGER.debug("WebSocket Client received closing");
        ch.close();
    }
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) 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) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)

Example 17 with WebSocketFrame

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

the class EncoderHandler method handleWebsocket.

private void handleWebsocket(final OutPacketMessage msg, ChannelHandlerContext ctx, ChannelPromise promise) throws IOException {
    ChannelFutureList writeFutureList = new ChannelFutureList();
    while (true) {
        Queue<Packet> queue = msg.getClientHead().getPacketsQueue(msg.getTransport());
        Packet packet = queue.poll();
        if (packet == null) {
            writeFutureList.setChannelPromise(promise);
            break;
        }
        final ByteBuf out = encoder.allocateBuffer(ctx.alloc());
        encoder.encodePacket(packet, out, ctx.alloc(), true);
        WebSocketFrame res = new TextWebSocketFrame(out);
        if (log.isTraceEnabled()) {
            log.trace("Out message: {} sessionId: {}", out.toString(CharsetUtil.UTF_8), msg.getSessionId());
        }
        if (out.isReadable()) {
            writeFutureList.add(ctx.channel().writeAndFlush(res));
        } else {
            out.release();
        }
        for (ByteBuf buf : packet.getAttachments()) {
            ByteBuf outBuf = encoder.allocateBuffer(ctx.alloc());
            outBuf.writeByte(4);
            outBuf.writeBytes(buf);
            if (log.isTraceEnabled()) {
                log.trace("Out attachment: {} sessionId: {}", ByteBufUtil.hexDump(outBuf), msg.getSessionId());
            }
            writeFutureList.add(ctx.channel().writeAndFlush(new BinaryWebSocketFrame(outBuf)));
        }
    }
}
Also used : Packet(com.corundumstudio.socketio.protocol.Packet) 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) ByteBuf(io.netty.buffer.ByteBuf)

Example 18 with WebSocketFrame

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

the class PerMessageDeflateDecoderTest method testDecompressionSkipForBinaryFrame.

@Test
public void testDecompressionSkipForBinaryFrame() {
    EmbeddedChannel encoderChannel = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.NONE, 9, 15, 8));
    EmbeddedChannel decoderChannel = new EmbeddedChannel(new PerMessageDeflateDecoder(false, ALWAYS_SKIP));
    byte[] payload = new byte[300];
    random.nextBytes(payload);
    assertTrue(encoderChannel.writeOutbound(Unpooled.wrappedBuffer(payload)));
    ByteBuf compressedPayload = encoderChannel.readOutbound();
    BinaryWebSocketFrame compressedBinaryFrame = new BinaryWebSocketFrame(true, WebSocketExtension.RSV1, compressedPayload);
    assertTrue(decoderChannel.writeInbound(compressedBinaryFrame));
    WebSocketFrame inboundFrame = decoderChannel.readInbound();
    assertEquals(WebSocketExtension.RSV1, inboundFrame.rsv());
    assertEquals(compressedPayload, inboundFrame.content());
    assertTrue(inboundFrame.release());
    assertTrue(encoderChannel.finishAndReleaseAll());
    assertFalse(decoderChannel.finish());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) 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) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 19 with WebSocketFrame

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

the class PerMessageDeflateDecoderTest method testSelectivityDecompressionSkip.

@Test
public void testSelectivityDecompressionSkip() {
    WebSocketExtensionFilter selectivityDecompressionFilter = new WebSocketExtensionFilter() {

        @Override
        public boolean mustSkip(WebSocketFrame frame) {
            return frame instanceof TextWebSocketFrame && frame.content().readableBytes() < 100;
        }
    };
    EmbeddedChannel encoderChannel = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.NONE, 9, 15, 8));
    EmbeddedChannel decoderChannel = new EmbeddedChannel(new PerMessageDeflateDecoder(false, selectivityDecompressionFilter));
    String textPayload = "compressed payload";
    byte[] binaryPayload = new byte[300];
    random.nextBytes(binaryPayload);
    assertTrue(encoderChannel.writeOutbound(Unpooled.wrappedBuffer(textPayload.getBytes(UTF_8))));
    assertTrue(encoderChannel.writeOutbound(Unpooled.wrappedBuffer(binaryPayload)));
    ByteBuf compressedTextPayload = encoderChannel.readOutbound();
    ByteBuf compressedBinaryPayload = encoderChannel.readOutbound();
    TextWebSocketFrame compressedTextFrame = new TextWebSocketFrame(true, WebSocketExtension.RSV1, compressedTextPayload);
    BinaryWebSocketFrame compressedBinaryFrame = new BinaryWebSocketFrame(true, WebSocketExtension.RSV1, compressedBinaryPayload);
    assertTrue(decoderChannel.writeInbound(compressedTextFrame));
    assertTrue(decoderChannel.writeInbound(compressedBinaryFrame));
    TextWebSocketFrame inboundTextFrame = decoderChannel.readInbound();
    BinaryWebSocketFrame inboundBinaryFrame = decoderChannel.readInbound();
    assertEquals(WebSocketExtension.RSV1, inboundTextFrame.rsv());
    assertEquals(compressedTextPayload, inboundTextFrame.content());
    assertTrue(inboundTextFrame.release());
    assertEquals(0, inboundBinaryFrame.rsv());
    assertArrayEquals(binaryPayload, ByteBufUtil.getBytes(inboundBinaryFrame.content()));
    assertTrue(inboundBinaryFrame.release());
    assertTrue(encoderChannel.finishAndReleaseAll());
    assertFalse(decoderChannel.finish());
}
Also used : TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) WebSocketExtensionFilter(io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionFilter) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) 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) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 20 with WebSocketFrame

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

the class PerMessageDeflateEncoderTest method testCompressionSkipForBinaryFrame.

@Test
public void testCompressionSkipForBinaryFrame() {
    EmbeddedChannel encoderChannel = new EmbeddedChannel(new PerMessageDeflateEncoder(9, 15, false, ALWAYS_SKIP));
    byte[] payload = new byte[300];
    random.nextBytes(payload);
    WebSocketFrame binaryFrame = new BinaryWebSocketFrame(Unpooled.wrappedBuffer(payload));
    assertTrue(encoderChannel.writeOutbound(binaryFrame.copy()));
    WebSocketFrame outboundFrame = encoderChannel.readOutbound();
    assertEquals(0, outboundFrame.rsv());
    assertArrayEquals(payload, ByteBufUtil.getBytes(outboundFrame.content()));
    assertTrue(outboundFrame.release());
    assertFalse(encoderChannel.finish());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) 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) 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