我要求根据登录名和客户端连接到的主机名/端口对连接数量进行严格限制。
对方法有什么想法吗?
发布于 2012-04-17 23:59:34
我认为你可以使用ChannelGroup来跟踪连接。根据channelGroup的内容,决定是否限制连接。请参阅下面的代码片段。所有添加到channelGroup中的通道在关闭时都会自动删除。
class YourHandler extends SimpleChannelHandler {
ChannelGroup channelGroup = new DefaultChannelGroup();
@Override
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
{
// make a decision if you want to accept connection
// if not just close it using ctc.getChannel().close()
}
@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
{
channelGroup.add(ctx.getChannel());
}
}https://stackoverflow.com/questions/10194008
复制相似问题