Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netty - how to get all client channel?

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)

like image 322
chentingpc Avatar asked Apr 29 '12 14:04

chentingpc


1 Answers

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.

like image 149
Nicholas Avatar answered Sep 23 '22 00:09

Nicholas