简体   繁体   中英

Netty SimpleChannelInboundHandler close channel

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?

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() .

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM