Netty - how to get all client channel?
java, netty, networking
Solution
For Netty 4.0.X
In main Class you need to declare the ChannelGroup object:
final ChannelGroup channels =
new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
When a new client is connected (you should pass the channels object in the constructor to you handler class):
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
channels.add(ctx.channel());
}
To get all clients, just iterate the channels object:
for (Channel ch : channels) {
//do something with ch object
}
Hope it helps.
Problem
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)