Actually I tried to make chatting program with Netty
I was very happy to know netty and it seems to be very good for speed and connections
but I faced with some problem
When Android Client side Stop application exit it Serverside relize that client is disconnected.
but when client change 4g to wifi client connection lost, however Serverside dosen't know about client lost.
noting happend.
so I google and found some good handler .
this is ping periodically .
and server side finally exactly know whether specific client is where alive or not.
=== Explain ==
idleStateHandler
readerIdleTime | an IdleStateEvent whose state is IdleState.READER_IDLE will be triggered when no read was performed for the specified period of time. Specify 0 to disable. |
writerIdleTime | an IdleStateEvent whose state is IdleState.WRITER_IDLE will be triggered when no write was performed for the specified period of time. Specify 0 to disable. |
allIdleTime | an IdleStateEvent whose state is IdleState.ALL_IDLE will be triggered when neither read nor write was performed for the specified period of time. Specify 0 to disable. |
pipeline.addLast("idleStateHandler", new IdleStateHandler(60, 30, 0)); pipeline.addLast("myHandler", new MyHandler());
class MyHandler extends ChannelDuplexHandler {
@Override
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("reader ");
System.out.println("reader ");
System.out.println("reader ");
System.out.println("reader ");
ctx.close();
} else if (e.state() == IdleState.WRITER_IDLE) {
try{
ctx.channel().writeAndFlush("ping:"+ctx.channel().hashCode() );
}catch(Exception es){
es.printStackTrace();
}
System.out.println("writer ");
System.out.println("writer ");
}
}
}
}
No comments:
Post a Comment