简体   繁体   中英

How to remove the channel from pipeline when client(browser) exits in netty?

Many clients(browser) can connect to my server(netty-server) via websockets. What i want is that whenever a client closes the tab(the url on which he was connected to my server), then the channel associated with him should also get Unregistered.But this is not happening automatically because i have created a hashset and added the clients ip to the hash set in channelRegistered(..) method and removed the ip from hashset in channelUnregistered(..) method. But it is not getting removed. Please let me know if there is another way to Unregister a channel whenever the client exits the browser(or the tab by which he is connected).Thanks.

channelRegistered() and channelUnregistered() are the wrong methods for this purpose. The purpose of those methods are to register when the handler itself is added/removed from a channel pipeline . While the registration is called, the unregistration isn't called because the pipeline keeps existing after the channel is closed.

You should use channelActive() and channelInactive() for the purpose you are wanting them. ChannelActive is triggered when the channel is active for sending, with a server this usually happens directly after channelRegistered and with a client it happens after the socket connection is made. The channelInactive() is called when close() is called on the local socket, or the remote end closed/killed the connection, you can count on this method to do local cleanup.

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