List of usage examples for io.netty.handler.codec.http.websocketx ContinuationWebSocketFrame ContinuationWebSocketFrame
public ContinuationWebSocketFrame(boolean finalFragment, int rsv, String text)
From source file:c5db.client.codec.WebsocketProtostuffEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext channelHandlerContext, Call call, List<Object> objects) throws Exception { final LowCopyProtobufOutput lcpo = new LowCopyProtobufOutput(); Call.getSchema().writeTo(lcpo, call); final long size = lcpo.buffer.size(); if (size < MAX_SIZE) { ByteBuf byteBuf = channelHandlerContext.alloc().buffer((int) size); lcpo.buffer.finish().stream().forEach(byteBuf::writeBytes); final BinaryWebSocketFrame frame = new BinaryWebSocketFrame(byteBuf); objects.add(frame);//from w w w. j a v a 2 s . c o m } else { long remaining = size; boolean first = true; ByteBuf byteBuf = channelHandlerContext.alloc().buffer((int) size); lcpo.buffer.finish().stream().forEach(byteBuf::writeBytes); while (remaining > 0) { WebSocketFrame frame; if (remaining > MAX_SIZE) { final ByteBuf slice = byteBuf.copy((int) (size - remaining), (int) MAX_SIZE); if (first) { frame = new BinaryWebSocketFrame(false, 0, slice); first = false; } else { frame = new ContinuationWebSocketFrame(false, 0, slice); } remaining -= MAX_SIZE; } else { frame = new ContinuationWebSocketFrame(true, 0, byteBuf.copy((int) (size - remaining), (int) remaining)); remaining = 0; } objects.add(frame); } } }
From source file:c5db.codec.WebsocketProtostuffEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext channelHandlerContext, Response response, List<Object> objects) throws IOException { final LowCopyProtobufOutput lcpo = new LowCopyProtobufOutput(); Response.getSchema().writeTo(lcpo, response); final long size = lcpo.buffer.size(); ByteBuf byteBuf = channelHandlerContext.alloc().buffer((int) size); lcpo.buffer.finish().stream().forEach(byteBuf::writeBytes); if (size < MAX_SIZE) { final BinaryWebSocketFrame frame = new BinaryWebSocketFrame(byteBuf); objects.add(frame);/* w w w .j a va 2 s. c o m*/ } else { long remaining = size; boolean first = true; while (remaining > 0) { WebSocketFrame frame; if (remaining > MAX_SIZE) { final ByteBuf slice = byteBuf.copy((int) (size - remaining), (int) MAX_SIZE); if (first) { frame = new BinaryWebSocketFrame(false, 0, slice); first = false; } else { frame = new ContinuationWebSocketFrame(false, 0, slice); } remaining -= MAX_SIZE; } else { frame = new ContinuationWebSocketFrame(true, 0, byteBuf.copy((int) (size - remaining), (int) remaining)); remaining = 0; } objects.add(frame); } } }
From source file:com.guowl.websocket.stream.client.StreamClientRunner.java
License:Apache License
public void run() throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {// w ww .j av a2 s .c om // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 // or V00. // If you change it to V00, ping is not supported and remember to // change // HttpResponseDecoder to WebSocketHttpResponseDecoder in the // pipeline. final StreamClientHandler handler = new StreamClientHandler(WebSocketClientHandshakerFactory .newHandshaker(uri, WebSocketVersion.V13, null, false, new DefaultHttpHeaders())); final String protocol = uri.getScheme(); int defaultPort; ChannelInitializer<SocketChannel> initializer; // Normal WebSocket if ("ws".equals(protocol)) { initializer = new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-codec", new HttpClientCodec()) .addLast("aggregator", new HttpObjectAggregator(8192)) .addLast("ws-handler", handler); } }; defaultPort = 80; // Secure WebSocket } else { throw new IllegalArgumentException("Unsupported protocol: " + protocol); } Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(initializer); int port = uri.getPort(); // If no port was specified, we'll try the default port: if (uri.getPort() == -1) { port = defaultPort; } Channel ch = b.connect(uri.getHost(), port).sync().channel(); handler.handshakeFuture().sync(); ByteBuf dataBuf = ch.alloc().buffer(); dataBuf.writeBytes("start".getBytes()); ch.writeAndFlush(new BinaryWebSocketFrame(false, 0, dataBuf)); ch.writeAndFlush(new ContinuationWebSocketFrame(true, 0, "end")); Thread.sleep(1000 * 60 * 60); } finally { group.shutdownGracefully(); } }
From source file:io.advantageous.conekt.http.impl.ConektHttpHandler.java
License:Open Source License
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { if (msg instanceof WebSocketFrameInternal) { WebSocketFrameInternal frame = (WebSocketFrameInternal) msg; ByteBuf buf = frame.getBinaryData(); if (buf != Unpooled.EMPTY_BUFFER) { buf = safeBuffer(buf, ctx.alloc()); }//from w ww. jav a 2s.c o m switch (frame.type()) { case BINARY: msg = new BinaryWebSocketFrame(frame.isFinal(), 0, buf); break; case TEXT: msg = new TextWebSocketFrame(frame.isFinal(), 0, buf); break; case CLOSE: msg = new CloseWebSocketFrame(true, 0, buf); break; case CONTINUATION: msg = new ContinuationWebSocketFrame(frame.isFinal(), 0, buf); break; case PONG: msg = new PongWebSocketFrame(buf); break; case PING: msg = new PingWebSocketFrame(buf); break; default: throw new IllegalStateException("Unsupported websocket msg " + msg); } } ctx.write(msg, promise); }
From source file:io.reactivex.netty.protocol.http.websocket.WebSocketClientServerTest.java
License:Apache License
@Test public void testFragmentedMessage() throws Exception { TestSequenceExecutor executor = new TestSequenceExecutor() .withClientFrames(new TextWebSocketFrame(false, 0, "first"), new ContinuationWebSocketFrame(false, 0, "middle"), new ContinuationWebSocketFrame(true, 0, "last")) .withExpectedOnServer(3).execute(); assertEquals("Expected first frame content", "first", asText(executor.getReceivedClientFrames().get(0))); assertEquals("Expected first frame content", "middle", asText(executor.getReceivedClientFrames().get(1))); assertEquals("Expected first frame content", "last", asText(executor.getReceivedClientFrames().get(2))); }
From source file:io.reactivex.netty.protocol.http.websocket.WebSocketClientServerTest.java
License:Apache License
@Test public void testMessageAggregationOnServer() throws Exception { TestSequenceExecutor executor = new TestSequenceExecutor().withMessageAggregation(true) .withClientFrames(new TextWebSocketFrame(false, 0, "0123456789"), new ContinuationWebSocketFrame(true, 0, "ABCDEFGHIJ")) .withExpectedOnServer(1).execute(); assertEquals("Expected aggregated message", "0123456789ABCDEFGHIJ", asText(executor.getReceivedClientFrames().get(0))); }
From source file:io.reactivex.netty.protocol.http.websocket.WebSocketClientServerTest.java
License:Apache License
@Test public void testMessageAggregationOnClient() throws Exception { TestSequenceExecutor executor = new TestSequenceExecutor().withMessageAggregation(true) .withServerFrames(new TextWebSocketFrame(false, 0, "0123456789"), new ContinuationWebSocketFrame(true, 0, "ABCDEFGHIJ")) .withExpectedOnClient(1).execute(); assertEquals("Expected aggregated message", "0123456789ABCDEFGHIJ", asText(executor.getReceivedServerFrames().get(0))); }
From source file:io.vertx.core.http.impl.Http1xConnectionBase.java
License:Open Source License
WebSocketFrame encodeFrame(WebSocketFrameImpl frame) { ByteBuf buf = frame.getBinaryData(); if (buf != Unpooled.EMPTY_BUFFER) { buf = safeBuffer(buf, chctx.alloc()); }//from ww w. j a v a2 s.com switch (frame.type()) { case BINARY: return new BinaryWebSocketFrame(frame.isFinal(), 0, buf); case TEXT: return new TextWebSocketFrame(frame.isFinal(), 0, buf); case CLOSE: return new CloseWebSocketFrame(true, 0, buf); case CONTINUATION: return new ContinuationWebSocketFrame(frame.isFinal(), 0, buf); case PONG: return new PongWebSocketFrame(buf); case PING: return new PingWebSocketFrame(buf); default: throw new IllegalStateException("Unsupported websocket msg " + frame); } }
From source file:org.ballerinalang.test.util.websocket.client.WebSocketTestClient.java
License:Open Source License
/** * Send text to the server.//from w w w . j a va 2 s . co m * * @param text text to be sent. * @param isFinal whether the text is final * @throws InterruptedException if connection is interrupted while sending the message. */ public void sendText(String text, boolean isFinal) throws InterruptedException { if (channel == null) { logger.error("Channel is null. Cannot send text."); throw new IllegalArgumentException("Cannot find the channel to write"); } if (!isFinal) { if (first) { channel.writeAndFlush(new TextWebSocketFrame(false, 0, text)).sync(); first = false; } else { channel.writeAndFlush(new ContinuationWebSocketFrame(false, 0, text)); } } else { channel.writeAndFlush(new ContinuationWebSocketFrame(true, 0, text)); first = true; } }
From source file:org.ballerinalang.test.util.websocket.client.WebSocketTestClient.java
License:Open Source License
/** * Send binary to the server.//from www .j a v a 2 s . c o m * * @param buffer buffer containing the data to be sent. * @param isFinal whether the text is final * @throws InterruptedException if connection is interrupted while sending the message. */ public void sendBinary(ByteBuffer buffer, boolean isFinal) throws InterruptedException { if (channel == null) { logger.error("Channel is null. Cannot send text."); throw new IllegalArgumentException("Cannot find the channel to write"); } if (!isFinal) { if (first) { channel.writeAndFlush(new BinaryWebSocketFrame(false, 0, Unpooled.wrappedBuffer(buffer))).sync(); first = false; } else { channel.writeAndFlush(new ContinuationWebSocketFrame(false, 0, Unpooled.wrappedBuffer(buffer))); } } else { channel.writeAndFlush(new ContinuationWebSocketFrame(true, 0, Unpooled.wrappedBuffer(buffer))); first = true; } }