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(boolean finalFragment, int rsv, ByteBuf binaryData) 

Source Link

Document

Creates a new binary frame with the specified binary data and the final fragment flag.

Usage

From source file:com.guowl.websocket.stream.client.StreamClientRunner.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {/*from www .  j  a v a  2  s .  com*/
        // 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());
        }//www.  jav  a  2  s.c om
        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.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   w  ww.ja  v a  2s  . c  om
    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.asynchttpclient.netty.ws.NettyWebSocket.java

License:Open Source License

@Override
public WebSocket stream(byte[] fragment, boolean last) {
    channel.writeAndFlush(new BinaryWebSocketFrame(last, 0, wrappedBuffer(fragment)), channel.voidPromise());
    return this;
}

From source file:org.asynchttpclient.netty.ws.NettyWebSocket.java

License:Open Source License

@Override
public WebSocket stream(byte[] fragment, int offset, int len, boolean last) {
    channel.writeAndFlush(new BinaryWebSocketFrame(last, 0, wrappedBuffer(fragment, offset, len)),
            channel.voidPromise());//  w  ww . jav  a2s.  c o m
    return this;
}

From source file:org.asynchttpclient.providers.netty4.ws.NettyWebSocket.java

License:Open Source License

@Override
public WebSocket stream(byte[] fragment, boolean last) {
    channel.writeAndFlush(new BinaryWebSocketFrame(last, 0, wrappedBuffer(fragment)));
    return this;
}

From source file:org.asynchttpclient.providers.netty4.ws.NettyWebSocket.java

License:Open Source License

@Override
public WebSocket stream(byte[] fragment, int offset, int len, boolean last) {
    channel.writeAndFlush(new BinaryWebSocketFrame(last, 0, wrappedBuffer(fragment, offset, len)));
    return this;
}

From source file:org.ballerinalang.test.util.websocket.client.WebSocketTestClient.java

License:Open Source License

/**
 * Send binary to the server.//from   w w  w .jav  a2  s.co 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;
    }
}

From source file:org.wso2.carbon.transport.http.netty.internal.websocket.WebSocketBasicRemoteEndpoint.java

License:Open Source License

@Override
public void sendBinary(ByteBuffer partialByte, boolean isLast) throws IOException {
    ByteBuf partialByteBuf = Unpooled.wrappedBuffer(partialByte);
    ctx.channel().writeAndFlush(new BinaryWebSocketFrame(isLast, 0, partialByteBuf));
}