crazyfacka
crazyfacka

Reputation: 3

Netty IdleStateEvent doesn't raise READER_IDLE on server

I'm creating a Netty (4.0.14 Final) client/server application with SSL Sockets and I am trying to implement an IdleStateEvent so that I can know when my clients or server goes offline.

The problem is, READER_IDLE does not work on the server. I'm pasting here snippets of the code.

SocketInitializer

public class SocketInitializer extends ChannelInitializer<SocketChannel> {

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    SSLEngine engine = SslContextFactory.getServerContext().createSSLEngine();
    engine.setUseClientMode(false);

    pipeline.addLast("idleHandler", new IdleStateHandler(30, 0, 0));
    pipeline.addLast("ssl", new SslHandler(engine));
    pipeline.addLast(new SocketDecoder(), new SocketHandler());
}
}

SocketHandler

(...)
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if(evt instanceof IdleState) {
        IdleState e = (IdleState) evt;
        if(e == IdleState.READER_IDLE) {
            System.out.println("It's idle!");
        }
    }
}
(...)

Now, I've tried to have the userEventTriggered() method both overridden on the IdleStateHandler class and on the SslHandler class (one at a time, of course :)), and neither of these options work. I have the exact same pipeline on the client with the userEventTriggered() on the SocketHandler class, but only triggering the WRITER_IDLE, and it works!

Is there anything that I am missing? Thank you for your time!

Upvotes: 0

Views: 1185

Answers (1)

Norman Maurer
Norman Maurer

Reputation: 23567

You need to check if it is of instance IdleStateEvent and if so cast it and check its state().

public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if(evt instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) evt;
        if(e.state() == IdleState.READER_IDLE) {
            System.out.println("It's idle!");
        }
    }
} 

See the example in the javadocs: https://github.com/netty/netty/blob/4.0/handler/src/main/java/io/netty/handler/timeout/IdleStateHandler.java#L77

Upvotes: 1

Related Questions