Reputation: 129
I am using netty HexDumpProxy example(using Netty 5 lib),in that I want to send some messages to server for each 40 seconds. How to achieve this using Netty. Help me to solve this.
Update: here is my initChannel method,
protected void initChannel(SocketChannel sc) throws Exception {
int readerIdleTimeSeconds = 5;
int writerIdleTimeSeconds = 5;
int allIdleTimeSeconds = 0;
ChannelPipeline pipe = sc.pipeline();
// pipe.addLast("rtspdecoder", new RtspRequestDecoder());
// pipe.addLast("rtspencoder", new RtspResponseEncoder());
// pipe.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
// pipe.addLast("encoder", new StringEncoder());
// pipe.addLast("decoder", new StringDecoder());
// pipe.addLast("idleStateHandler", new IdleStateHandler(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds));
// pipe.addLast("idleStateEventHandler", new MyIdlestaeEvtHandler());
pipe.addLast("decoder", new MyRtspRequestDecoder());
pipe.addLast("encoder", new MyRtspResponseEncoder());
pipe.addLast("handler", new PServerRequestHandler(remoteHost, remotePort));
pipe.addLast("idleStateHandler", new IdleStateHandler(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds));
pipe.addLast("idleStateEventHandler", new MyIdlestaeEvtHandler());
}
here is MyIdlestaeEvtHandler class,
public class MyIdlestaeEvtHandler extends ChannelDuplexHandler {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if(evt instanceof IdleStateEvent) {
IdleStateEvent e = (IdleStateEvent) evt;
if(e.state() == IdleState.WRITER_IDLE) {
String s = "Ping Pong!";
ctx.channel().writeAndFlush(Unpooled.copiedBuffer(s.getBytes()));
System.err.println("writing idle------------------------");
} else if(e.state() == IdleState.READER_IDLE) {
System.err.println("reading idle------------------------");
String s = "Pong Pong!";
ctx.channel().writeAndFlush(Unpooled.copiedBuffer(s.getBytes()));
}
}
}
}
I am able to see the writing idle------------------------ but, the same is not passed to server, because I am not able to see this message in server debug messages. Anything wrong with the code?
ThankYou
Upvotes: 0
Views: 1200
Reputation: 319
Netty 4.x provides the capability to schedule an instance of Runnable to be executed at a fixed rate using the scheduleAtFixedRate() method on a channel's eventloop.
see javadoc for scheduleAtFixedRate() in EventExecutorGroup
Example:
channel.eventLoop().scheduleAtFixedRate(new Runnable(){
@Override
public void run()
{
System.out.println("print a line every 10 seconds with a 5 second initial delay");
}
}, 5, 10, TimeUnit.SECONDS);
Upvotes: 1
Reputation: 23557
Use IdleStateHandler. For more informations please check its javadocs.
Upvotes: 0