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();
}
}
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)));
}
}
}
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());
}
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());
}
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());
}
Aggregations