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

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

Introduction

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

Prototype

public PingWebSocketFrame() 

Source Link

Document

Creates a new empty ping frame.

Usage

From source file:com.codebullets.external.party.simulator.connections.websocket.outbound.KeepAliveHandler.java

License:Apache License

/**
 * Triggered by the IdleStateHandler in the channel pipeline. Indicates that either
 * no message has been either sent or received for a longer period of time.
 *///from  w  w w .  j  a v a 2  s.com
@Override
public void userEventTriggered(final ChannelHandlerContext ctx, final Object evt) {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) evt;
        if (e.state() == IdleState.WRITER_IDLE && isAlive) {
            // no message has been sent for longer period of time -> send ping to keep connection alive
            ctx.writeAndFlush(new PingWebSocketFrame());
        } else if (e.state() == IdleState.READER_IDLE) {
            // no message has been received for a longer period of time
            // this means even no pong events.
            LOG.warn("No message received in expected time.");
            ctx.close();
        }
    }
}

From source file:io.crossbar.autobahn.wamp.transports.NettyWebSocketClientHandler.java

License:MIT License

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state() == IdleState.READER_IDLE) {
            close(ctx, false, new CloseDetails(CloseDetails.REASON_TRANSPORT_LOST, null));
        } else if (event.state() == IdleState.WRITER_IDLE) {
            ctx.writeAndFlush(new PingWebSocketFrame());
        }/*from  w  w  w  .  j  av a 2 s.co m*/
    }
}

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  a v  a2s . co m*/

    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.App.java

License:Open Source License

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        switch (((IdleStateEvent) evt).state()) {
        case ALL_IDLE: {
            Platform.shelloidLogger.debug("Client sending PingWebSocketFrame to " + ctx.channel());
            ctx.channel().writeAndFlush(new PingWebSocketFrame());
            break;
        }//from ww w  . j  av  a 2  s.com
        case READER_IDLE: {
            Platform.shelloidLogger.debug("Read Idle for " + (Configurations.PING_SEND_INTERVAL * 2)
                    + " seconds. So closing the channel: " + ctx.channel());
            ctx.channel().close();
            break;
        }
        }
    }
}

From source file:org.shelloid.vpt.rms.server.VPTServer.java

License:Open Source License

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        switch (((IdleStateEvent) evt).state()) {
        case ALL_IDLE: {
            Platform.shelloidLogger.debug("Server sending PingWebSocketFrame to " + ctx.channel());
            ctx.channel().writeAndFlush(new PingWebSocketFrame());
            break;
        }//from  w ww. j a  va2  s. c om
        case READER_IDLE: {
            Platform.shelloidLogger.debug("Read Idle for " + (Configurations.PING_SEND_INTERVAL * 2)
                    + " seconds. So closing the channel: " + ctx.channel());
            ctx.channel().close();
            break;
        }
        }
    }
}

From source file:org.thingsplode.synapse.proxy.handlers.WSMessageDecoder.java

License:Apache License

@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    handshakeFuture = ctx.newPromise();/*  w  w  w.j a  va  2s .  c o m*/
    handshakeFuture.addListener((ChannelFutureListener) (ChannelFuture future) -> {
        if (future.isSuccess()) {
            //websocket HANDSHAKE was successfull, scheduing pinging
            pingScheduledFuture = ctx.channel().eventLoop().scheduleAtFixedRate(() -> {
                ctx.writeAndFlush(new PingWebSocketFrame());
            }, 0, 60, TimeUnit.SECONDS);
        }
    });

}