Our scenario is as follow:
- There's a TCP server written using Netty.
- There are multiple clients establishing a connection to the server.
- After establishing the connection, messages can be sent in the two direction.
To be able to deliver messages to correct recipients I need to maintain channel outside of handlers. I'm aware of ChannelGroup
which let me easily implement a broadcast server (one message to all clients).
Now the requirement is how to write to specific channel.
I'm thinking to have a ChannelGroup
and enrich it with client-specific id map (with a Map<ClientId, ChannelId>
mapping client-id to channel-id). This way having a client-id, I can easily get the channel and write to it.
Now my question is:
- Generally what is the different between writing via
ChannelHandlerContext
and viaChannel
? (sinceChannelGroup
gives me theChannel
notChannelHandlerContext
). - Is it a bad idea to maintain the group of
ChannelHandlerContext
? (in other words, why don't we haveChannelHandlerContextGroup
)? - Any other suggestion how to do it?