Netty Channel closed detection

后端 未结 3 1629
深忆病人
深忆病人 2020-12-28 20:23

I am building a server client application using netty and ios, I am facing a problem when the user just turns off WiFi on his/her ios device, the netty server does not know

3条回答
  •  死守一世寂寞
    2020-12-28 20:44

    If I understood your problem correctly: You want to listen for client channel closed events in server side and do some session cleanup,

    There are two ways to listen for channel closed events in Netty :

    1) If your server handler extends SimpleChannelHandler/SimpleChannelHandler, then you can override following method and write your session cleanup logic there

    public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception;
    

    2) If you have only access to the channel reference, then you can get the channel close future and register your implementation of ChannelFutureListener with your session cleanup logic,

    ChannelFuture closeFuture = channel.closeFuture();
    
    closeFuture.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            //session cleanup logic
        }
    });
    

提交回复
热议问题