Reputation: 179
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?
Upvotes: 6
Views: 5879
Reputation: 77
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.
Upvotes: 0
Reputation: 12351
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()
.
Upvotes: 5
Reputation: 119
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>
Upvotes: -1