4
votes

I have handler that extends SimpleChannelInboundHandler. I use it for handling websocket frames. What is the best way to detect that connection was closed from client side (for browser closed or network connection crashed)? I see there are two methods in ChannelInboundHandlerAdapter: channelUnregistered and channelInactive. Can I use its for detectiong channel closing?

3

3 Answers

5
votes

You should use channelInactive() which is triggered when a channel cannot perform communication anymore. channelUnregistered() has different meaning although channelUnregistered() is always triggered after channelInactive().

0
votes

channelUnregistered() is also called in case of an unsuccessful connection attempt. So channelInactive() seems to be the better choice to listen to connection closed events.

I just saw this when implementing a reconnection strategy, where a closed connection would trigger a reconnect. channelUnregistered() was my first choice and obviously wrong, because it would re-trigger a reconnect after each reconnect, endlessly.

Btw: I also tested with websocket connections.

-1
votes

with netty 4 i use channelUnregistered

maybe you are worried about the warning on the log, i solved it changing the log level because it is already handled by the method but always appear

<logger name="io.netty" additivity="false">
    <level value="ERROR" />
    <appender-ref ref="console" />
</logger>