Example usage for io.netty.handler.codec.http.websocketx BinaryWebSocketFrame BinaryWebSocketFrame

List of usage examples for io.netty.handler.codec.http.websocketx BinaryWebSocketFrame BinaryWebSocketFrame

Introduction

In this page you can find the example usage for io.netty.handler.codec.http.websocketx BinaryWebSocketFrame BinaryWebSocketFrame.

Prototype

public BinaryWebSocketFrame(ByteBuf binaryData) 

Source Link

Document

Creates a new binary frame with the specified binary data.

Usage

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  ww. jav  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);//from   ww w.j a va 2s. co  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:catacumba.websocket.internal.DefaultWebSocket.java

License:Apache License

@Override
public CompletionStage<Void> send(ByteBuf data) {
    final CompletableFuture<Void> f = new CompletableFuture<Void>();
    channel.writeAndFlush(new BinaryWebSocketFrame(data)).addListener(future -> {
        if (future.isSuccess()) {
            f.complete(null);//w  ww  . ja v a  2s  .co  m
        } else {
            f.completeExceptionally(future.cause());
        }
    });

    return f;
}

From source file:cc.blynk.integration.model.websocket.AppWebSocketClient.java

License:Apache License

private static WebSocketFrame produceWebSocketFrame(MessageBase msg) {
    byte[] data = msg.getBytes();
    ByteBuf bb = ByteBufAllocator.DEFAULT.buffer(3 + data.length);
    bb.writeByte(msg.command);//  w  w  w. j  a v a  2 s . c om
    bb.writeShort(msg.id);
    bb.writeBytes(data);
    return new BinaryWebSocketFrame(bb);
}

From source file:cc.blynk.integration.model.websocket.WebSocketClient.java

License:Apache License

private static WebSocketFrame produceWebSocketFrame(MessageBase msg) {
    ByteBuf bb = PooledByteBufAllocator.DEFAULT.heapBuffer(5 + msg.length);
    bb.writeByte(msg.command);// w  w w .  j  a va2s .  c o  m
    bb.writeShort(msg.id);
    bb.writeShort(msg.length);
    byte[] data = msg.getBytes();
    if (data != null) {
        bb.writeBytes(data);
    }
    return new BinaryWebSocketFrame(bb);
}

From source file:com.athena.dolly.websocket.client.test.WebSocketClient.java

License:Apache License

public void sendFile(File file) throws Exception {
    // Send 10 messages and wait for responses
    System.out.println("WebSocket Client sending file ->" + file.getAbsolutePath());
    //for (int i = 0; i < 10; i++) {
    //    ch.writeAndFlush(new TextWebSocketFrame("Message #" + i));
    //}/*from w  ww  .  ja v  a2 s .  c  om*/

    ch.writeAndFlush(new TextWebSocketFrame(file.getAbsolutePath()));

    // Binary File Data Send
    byte[] buf = new byte[65536];
    int len = 0;

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

    while ((len = bis.read(buf)) != -1) {
        ch.write(new BinaryWebSocketFrame(Unpooled.copiedBuffer(buf, 0, len)));
    }

    ch.flush();
    System.out.println("File send succeed");

    bis.close();

    // Ping
    System.out.println("WebSocket Client sending ping");
    ch.writeAndFlush(new PingWebSocketFrame(Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 })));

}

From source file:com.barchart.netty.common.pipeline.WebSocketBinaryCodec.java

License:BSD License

@Override
protected void encode(final ChannelHandlerContext ctx, final ByteBuf msg, final List<Object> out)
        throws Exception {
    out.add(new BinaryWebSocketFrame(msg.retain()));
}

From source file:com.barchart.netty.server.http.websocket.WebSocketFramePacker.java

License:BSD License

@Override
protected void encode(final ChannelHandlerContext ctx, final ByteBuf msg, final List<Object> out)
        throws Exception {
    final BinaryWebSocketFrame binaryWebSocketFrame = new BinaryWebSocketFrame(msg);
    ctx.writeAndFlush(binaryWebSocketFrame);
}

From source file:com.codebullets.external.party.simulator.connections.websocket.inbound.InboundWebSocketConnection.java

License:Apache License

/**
 * Broadcasts a byte buffer message to all connected channels.
 *//*from  w  w w  .jav  a 2s.  c om*/
@Override
public void send(final byte[] buffer) {
    if (connectedChannels != null && !connectedChannels.isEmpty()) {
        ByteBuf binaryData = Unpooled.copiedBuffer(buffer);
        connectedChannels.writeAndFlush(new BinaryWebSocketFrame(binaryData));
    }
}

From source file:com.codebullets.external.party.simulator.connections.websocket.inbound.InboundWebSocketConnection.java

License:Apache License

/**
 * {@inheritDoc}//www .  ja v  a2  s  .com
 */
@Override
public void send(final ConnectionContext context, final byte[] buffer) {
    if (context instanceof NettyConnectionContext) {
        NettyConnectionContext nettyContext = (NettyConnectionContext) context;
        ByteBuf binaryData = Unpooled.copiedBuffer(buffer);
        nettyContext.getChannel().writeAndFlush(new BinaryWebSocketFrame(binaryData));
    } else {
        LOG.warn("Expected context of type NettyConnectionContext, but was "
                + context.getClass().getSimpleName());
    }
}