I was using netty example codes - telnet packet, Now the code can establish server and client to chat using telnet, but client can only talk to server. I am rewriting it to make the clients can talk to all the clients, so I need to keep a channel list, so when a client is contact the server, the server can send the message to all of the clients. Can anyone tell me how could I get all clients channel? (The example code is enter link description here)
Mauricio's proposal is a good one. In addition, the Netty API already provides a channel container in the ChannelGroup. It is thread safe and also provides several additional features such as group operations on all contained channels and automatic removal of contained channels when they are closed. From the javadoc:
A thread-safe Set that contains open Channels and provides various bulk operations on them. Using ChannelGroup, you can categorize Channels into a meaningful group (e.g. on a per-service or per-state basis.) A closed Channel is automatically removed from the collection, so that you don't need to worry about the life cycle of the added Channel. A Channel can belong to more than one ChannelGroup.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With