Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netty Channel closed detection

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.

like image 911
Khaled Abu AlKheir Avatar asked Jan 09 '12 09:01

Khaled Abu AlKheir


People also ask

What is ChannelHandlerContext?

A ChannelHandlerContext represents an association between a ChannelHandler and a ChannelPipeline and is created whenever a ChannelHandler is added to a ChannelPipeline .

Is Netty single threaded?

Netty client uses only one thread.

What is Netty pipeline?

The Netty ChannelPipeline is a very central concept in Netty. Each Netty SocketChannel contains a ChannelPipeline . The ChannelPipeline contains a list of ChannelHandler instances. These ChannelHandler instances are called when data is moving in and out of the SocketChannel .


4 Answers

well the question is 9 years old a lot has happened since then 2008 Economic Collapse, Wars, Coronavirus. On the bright side small Stock-Retailers seem to be winning the war against Hedge-funds on Wall-Street.

Back to the Question: We were facing a similar problem where we had to provide a log message on the server-side every-time a client would close the channel. The solutions provided above did not help but after 9 years a lot might have also changed in Netty.

Instead we extended our handler with the "MessagetoMessageDecoder" which also extends the ChannelInboundHandlerAdapter. We then override the void method "channelUnregistered" which triggers when a channel is unregistered from its EventLoop.

public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
    ctx.fireChannelUnregistered();
    //Your logic goes here...
}

This also works when the server itself closes the channel. Hope this helps somebody.

like image 58
Manfredovik Avatar answered Oct 04 '22 22:10

Manfredovik


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
    }
});
like image 39
Jestan Nirojan Avatar answered Oct 04 '22 21:10

Jestan Nirojan


Use IdleStateHandler

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

like image 39
jacktrades Avatar answered Oct 04 '22 23:10

jacktrades


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? )

like image 36
ikosdroid Avatar answered Oct 04 '22 21:10

ikosdroid