Trouble with Netty IdleStateHandler - am I testing it the wrong way?(Netty IdleStateHandler 的问题 - 我测试它的方式错误吗?)
问题描述
我有一个玩具 Netty 服务器,当他们的频道上没有发生任何事情时,我正在尝试向客户端发送心跳消息.我正在通过远程登录到服务器,写一条消息然后不发送任何东西来测试这个,但是我没有听到心跳!
I have a toy Netty server and am trying to send heartbeat messages to clients when nothing has happened on their channels. I am testing this by telnetting to the server, writing a message and then not sending anything, but I get no hearbeat!
控制台:
>>telnet localhost 6969
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
>>foo
Did you say 'foo'?
MyPipelineFactory.java
MyPipelineFactory.java
public class MyPipelineFactory implements ChannelPipelineFactory {
private final Timer timer;
private static final ChannelHandler stringDecoder = new StringDecoder();
private static final ChannelHandler stringEncoder = new StringEncoder();
private final ChannelHandler idleStateHandler;
public MyPipelineFactory(Timer t) {
this.timer = t;
this.idleStateHandler = new IdleStateHandler(timer, 5, 5, 5);
}
public ChannelPipeline getPipeline() {
// create default pipeline from static method
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("idleStateHandler", this.idleStateHandler); // heartbeat
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(1024, Delimiters.lineDelimiter()));
//pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1024,0,1)); // get header from message
pipeline.addLast("stringDecoder", stringDecoder);
pipeline.addLast("stringEncoder", stringEncoder);
pipeline.addLast("ServerHandler", new ServerHandler()); // goes at the end
return pipeline;
}
}
HeartbeatHandler.java
HeartbeatHandler.java
public class HeartbeatHandler extends IdleStateAwareChannelHandler {
@Override
public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) {
if (e.getState() == IdleState.READER_IDLE) {
System.out.println("Reader idle, closing channel");
//e.getChannel().close();
e.getChannel().write("heartbeat-reader_idle");
}
else if (e.getState() == IdleState.WRITER_IDLE) {
System.out.println("Writer idle, sending heartbeat");
e.getChannel().write("heartbeat-writer_idle");
}
else if (e.getState() == IdleState.ALL_IDLE) {
System.out.println("All idle, sending heartbeat");
e.getChannel().write("heartbeat-all_idle");
}
}
}
<小时>
固定:
我忘记了 HeartbeatHandler,它需要 IdleStateHandler(这部分对我来说并不明显).这行得通.
I forgot to have the HeartbeatHandler, which requires the IdleStateHandler (this part wasn't obvious to me). That works.
public class MyPipelineFactory implements ChannelPipelineFactory {
private final Timer timer;
private static final ChannelHandler stringDecoder = new StringDecoder();
private static final ChannelHandler stringEncoder = new StringEncoder();
private final ChannelHandler idleStateHandler;
private final ChannelHandler heartbeatHandler;
public MyPipelineFactory(Timer t) {
this.timer = t;
this.idleStateHandler = new IdleStateHandler(timer, 5, 5, 5);
this.heartbeatHandler = new HeartbeatHandler();
}
public ChannelPipeline getPipeline() {
// create default pipeline from static method
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("idleStateHandler", this.idleStateHandler);
pipeline.addLast("heartbeatHandler", this.heartbeatHandler); // heartbeat
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(1024, Delimiters.lineDelimiter()));
//pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1024,0,1)); // get header from message
pipeline.addLast("stringDecoder", stringDecoder);
pipeline.addLast("stringEncoder", stringEncoder);
pipeline.addLast("ServerHandler", new ServerHandler()); // goes at the end
return pipeline;
}
}
推荐答案
您错过了在 ChannelPipeline 中添加 HeartbeatHandler.您需要将 IdleStateHandler AND HeartbeatHandler 添加到 ChannelPipeline 以使其工作.
You missed to add the HeartbeatHandler in the ChannelPipeline. You need to add IdleStateHandler AND HeartbeatHandler to the ChannelPipeline to have it work.
这篇关于Netty IdleStateHandler 的问题 - 我测试它的方式错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Netty IdleStateHandler 的问题 - 我测试它的方式错误吗?
基础教程推荐
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01