我正在使用netty HexDumpProxy示例(使用Netty5lib),因为我希望每隔40秒向服务器发送一些消息。如何使用Netty实现这一点。帮我解决这个问题。
更新:这是我的initChannel方法,
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());
}下面是MyIdlestaeEvtHandler类,
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()));
}
}
}
}我可以看到写入idle-但是,同样的消息没有传递到服务器,因为我在服务器调试消息中看不到这条消息。代码有什么问题吗?
ThankYou
发布于 2016-10-14 23:59:13
Netty4.x提供了在通道的eventloop上使用scheduleAtFixedRate()方法来调度Runnable的实例以固定速率执行的功能。
see javadoc for scheduleAtFixedRate() in EventExecutorGroup
示例:
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);发布于 2014-04-28 16:33:04
使用IdleStateHandler。有关更多信息,请查看其javadoc。
https://stackoverflow.com/questions/23333916
复制相似问题