【发布时间】:2016-01-31 21:46:29
【问题描述】:
我使用 Netty 已经有一段时间了,但主要是在它们的通道始终唯一时使用普通套接字,因此我可以映射通道以了解谁正在连接到我的服务器。
现在我已经成功实现了 http 通信。问题是 ChannelHandlerContext 处理程序(以及来自这些处理程序的通道)的值不是唯一的,我无法检测到谁仅通过他们的处理程序进行连接。
问题:
这种行为(ChannelHandlerContext 处理程序值不是 唯一)正常还是我的代码有一些错误?
有什么想法和解决方案吗?
非常感谢
我的 ChannelInitializer 如下所示:
public class NettyHttpServerInitializer extends ChannelInitializer<SocketChannel> {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("http", new HttpServerCodec()));
pipeline.addLast("dechunker", new HttpObjectAggregator(65536));
pipeline.addLast("handler", new HttpServerHandler());
}
}
我的服务器处理程序看起来像(ctx 和 ctx.channel() 的值即使从同一个客户端触发也不是唯一的):
public class HttpServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
@Override
protected void messageReceived(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
...
}
}
【问题讨论】: