【发布时间】:2014-07-30 07:34:31
【问题描述】:
我需要根据客户端的用户名从 netty 服务器向客户端发送消息。 因此,我需要使用用户名映射频道并在每次要发送消息时找到频道。
我有两种方法,问题是: 就服务器端的性能而言,哪种方法更好。你有什么更好的主意吗?
-
在哈希图中使用用户名映射频道。
//Send userName from client side in first request //Get userName in server side and put it in a map Map<String, Channel> userMap = new ConcurrentHashMap<String,Channel>(); //loop over userMap to find specific client -
使用用户名设置附件。
//Set the attachment in client side ctx.getChannel().setAttachment(username); //Put all channels to a default channel group //Get all channels, search in their attachments to find specific client
【问题讨论】:
-
我认为您想根据用户名查找频道,而不是相反,所以您需要
Map<String, Channel>? -
第二种方法似乎要糟糕得多,因为“search”这个词,不过,第二种方法的变体似乎是可能的。现在我喜欢第一个。
-
@AndrewStubbs 感谢您的通知,已更正。
-
@keyser 从地图中删除频道怎么样?还是合乎逻辑的?
标签: java sockets networking network-programming netty