Netty Channel closed detection

2019-02-02 13:35发布

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 about it. The server needs to know to do cleanup for that user and set him/her offline, but now when the user tries to connect again, the server just tells him that he/she is already online.

3条回答
我只想做你的唯一
2楼-- · 2019-02-02 14:13

Check session id and allow renegotiation. Or you may use something like cookie controller. May I ask off topic question: How does your client on ios interact with netty server? (what framework you use on client side, and what decoder/encoder use? )

查看更多
疯言疯语
3楼-- · 2019-02-02 14:14

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
    }
});
查看更多
在下西门庆
4楼-- · 2019-02-02 14:22

Use IdleStateHandler

You can detect when there is no request/responses in given time intervals.

查看更多
登录 后发表回答