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

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

Introduction

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

Prototype

public PongWebSocketFrame() 

Source Link

Document

Creates a new empty pong frame.

Usage

From source file:io.haze.transport.tcp.websocket.WebSocketFrameDecoder.java

License:Apache License

/**
 * Handle a web socket frame.//w  ww  .  j  a  v a  2  s  .  c o m
 *
 * @param context  The channel handler context.
 * @param frame    The web socket frame.
 * @param messages The output messages.
 *
 * @throws Exception If an error has occurred.
 */
private void handleWebSocket(ChannelHandlerContext context, WebSocketFrame frame, List<Object> messages)
        throws Exception {
    if (frame instanceof TextWebSocketFrame || frame instanceof BinaryWebSocketFrame) {
        messages.add(frame.content().retain());
    } else if (frame instanceof CloseWebSocketFrame) {
        InetSocketAddress address = ((SocketChannel) context.channel()).remoteAddress();
        WebSocketServerHandshaker handshaker = handshakers.remove(address);

        handshaker.close(context.channel(), (CloseWebSocketFrame) frame.retain());
    } else if (frame instanceof PingWebSocketFrame) {
        context.writeAndFlush(new PongWebSocketFrame());
    }
}

From source file:io.reactivex.netty.protocol.http.websocket.WebSocketClientServerTest.java

License:Apache License

@Test
public void testPingPong() throws Exception {
    TestSequenceExecutor executor = new TestSequenceExecutor().withClientFrames(new PingWebSocketFrame())
            .withExpectedOnServer(1).withServerFrames(new PongWebSocketFrame()).withExpectedOnClient(1)
            .execute();/*from  w  w w  . j  ava 2s  .c om*/

    assertTrue("Expected ping on server",
            executor.getReceivedClientFrames().get(0) instanceof PingWebSocketFrame);
    assertTrue("Expected pong on client",
            executor.getReceivedServerFrames().get(0) instanceof PongWebSocketFrame);
}

From source file:org.shelloid.vpt.agent.VPTClient.java

License:Open Source License

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    Channel ch = ctx.channel();/*from w  ww.  j  av  a2 s. c  o  m*/
    if (!handshaker.isHandshakeComplete()) {
        handshaker.finishHandshake(ch, (FullHttpResponse) msg);
        handshakeFuture.setSuccess();
        onWsAuthenticated();
        Platform.shelloidLogger.debug("Client connected using " + ch + ". Now sending init ACK");
        sendAckMessage(ch, messenger.getLastSendAckNum());
        setChannel(ch);
        return;
    }

    if (msg instanceof FullHttpResponse) {
        FullHttpResponse response = (FullHttpResponse) msg;
        throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.getStatus()
                + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
    }

    WebSocketFrame frame = (WebSocketFrame) msg;
    if (frame instanceof BinaryWebSocketFrame) {
        BinaryWebSocketFrame binFrame = (BinaryWebSocketFrame) frame;
        ByteBuf b = binFrame.content();
        byte[] bytes = new byte[b.capacity()];
        b.getBytes(0, bytes);
        handleShelloidClientMsg(bytes, ctx.channel());

    } else if (frame instanceof TextWebSocketFrame) {
        throw new Exception("TextWebSocketFrame" + ((TextWebSocketFrame) frame).text());
    } else if (frame instanceof PingWebSocketFrame) {
        ctx.channel().writeAndFlush(new PongWebSocketFrame());
    } else if (frame instanceof PongWebSocketFrame) {
        Platform.shelloidLogger.info("WebSocket Client received pong");
    } else if (frame instanceof CloseWebSocketFrame) {
        Platform.shelloidLogger.info("WebSocket Client received closing");
        try {
            ch.close().sync();
            ctx.close().sync();
        } catch (InterruptedException ex) {
            Platform.shelloidLogger.error("InterruptedException while closing channel (channelInactive)");
        }
    } else {
        throw new Exception("Frame type not supported: " + msg);
    }
}

From source file:vn.com.vng.gsmobile.casino.client.ClientHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    Channel ch = ctx.channel();/*from  w w w  . ja  v a2  s.  c o  m*/
    if (!handshaker.isHandshakeComplete()) {
        handshaker.finishHandshake(ch, (FullHttpResponse) msg);
        Lib.getLogger().debug(Arrays.asList(uid, ctx.channel().id(), "connected",
                this.getClass().getSimpleName() + ".channelRead0"));
        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 BinaryWebSocketFrame) {
        receiptFormServer(ctx, frame);
    } else if (frame instanceof PingWebSocketFrame) {
        ch.writeAndFlush(new PongWebSocketFrame());
        Lib.getLogger().trace(Arrays.asList(uid, ctx.channel().id(), "pong:OK",
                this.getClass().getSimpleName() + ".channelRead0"));
    } else if (frame instanceof CloseWebSocketFrame) {
        Lib.getLogger().debug(Arrays.asList(uid, ctx.channel().id(), "received closing",
                this.getClass().getSimpleName() + ".channelRead0"));
        ch.close();
    }
}