Example usage for io.netty.handler.codec.http.websocketx CloseWebSocketFrame retain

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

Introduction

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

Prototype

@Override
    public CloseWebSocketFrame retain() 

Source Link

Usage

From source file:org.animotron.bridge.http.websocket.WebSocketHandler.java

License:Open Source License

public void close(WebSocketServerHandshaker hs, ChannelHandlerContext ctx, CloseWebSocketFrame frame) {
    hs.close(ctx.channel(), frame.retain());
}

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

License:Open Source License

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    Channel ch = ctx.channel();/* w  w w  .  j  ava 2 s.  co m*/
    if (!handshaker.isHandshakeComplete()) {
        FullHttpResponse fullHttpResponse = (FullHttpResponse) msg;
        headers = fullHttpResponse.headers();
        handshaker.finishHandshake(ch, fullHttpResponse);
        logger.info("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) + ')');
    }

    if (msg instanceof WebSocketFrame) {
        WebSocketFrame frame = (WebSocketFrame) msg;
        if (frame instanceof TextWebSocketFrame) {
            TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
            textReceived = textFrame.text();
        } else if (frame instanceof BinaryWebSocketFrame) {
            BinaryWebSocketFrame binaryFrame = (BinaryWebSocketFrame) frame;
            bufferReceived = binaryFrame.content().nioBuffer();
        } else if (frame instanceof PingWebSocketFrame) {
            PingWebSocketFrame pingFrame = (PingWebSocketFrame) frame;
            isPing = true;
            bufferReceived = pingFrame.content().nioBuffer();
        } else if (frame instanceof PongWebSocketFrame) {
            PongWebSocketFrame pongFrame = (PongWebSocketFrame) frame;
            isPong = true;
            bufferReceived = pongFrame.content().nioBuffer();
        } else if (frame instanceof CloseWebSocketFrame) {
            CloseWebSocketFrame closeWebSocketFrame = (CloseWebSocketFrame) frame;
            int statusCode = closeWebSocketFrame.statusCode();
            receivedCloseFrame = closeWebSocketFrame.retain();
            if (ch.isOpen()) {
                ch.writeAndFlush(new CloseWebSocketFrame(statusCode, null)).addListener(future -> {
                    if (ch.isOpen()) {
                        ch.close();
                    }
                }).sync();
            }
        }
        if (countDownLatch != null) {
            countDownLatch.countDown();
            countDownLatch = null;
        }
    }
}