关于netty的attributemap的问题

开源中国首席退堂鼓手 发布于 2018/12/25 12:32
阅读 264
收藏 0
public class AttributeMapConstant { public static final AttributeKey NETTY_CHANNEL_KEY = AttributeKey.valueOf("netty.channel"); } public class HelloWorldClientHandler extends ChannelInboundHandlerAdapter { @Override public void channelActive(ChannelHandlerContext ctx) { Attribute attr = ctx.attr(AttributeMapConstant.NETTY_CHANNEL_KEY); NettyChannel nChannel = attr.get(); if (nChannel == null) { NettyChannel newNChannel = new NettyChannel("HelloWorld0Client", new Date()); nChannel = attr.setIfAbsent(newNChannel); } else { System.out.println("channelActive33333 attributeMap 中是有值的"); System.out.println(nChannel.getName() + "3333=======" + nChannel.getCreateDate()); } System.out.println("HelloWorldC333333ientHandler Active"); ctx.fireChannelActive(); } public class HelloWorldServerHandler extends ChannelInboundHandlerAdapter {//这是server端口 @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { System.out.println("server channelRead.."); System.out.println(ctx.channel().remoteAddress() + "->Server :" + msg.toString()); String ccc = (String) ctx.channel().attr(AttributeKey.valueOf("netty.channel")).get(); System.out.println(ccc); ctx.write("server write" + msg); ctx.flush(); } 我在cilent端口设置了AttributeKey ,server端口能获取嘛,因为需求是需要知道cilent端的来源,以及信息,或者还有其他方法吗 用netty的T.T
加载中
0
开源中国首席退堂鼓手
开源中国首席退堂鼓手
public class HelloWorldServerHandler extends ChannelInboundHandlerAdapter {//这是server端口 @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { System.out.println("server channelRead.."); System.out.println(ctx.channel().remoteAddress() + "->Server :" + msg.toString()); String ccc = (String) ctx.channel().attr(AttributeKey.valueOf("netty.channel")).get(); System.out.println(ccc); ctx.write("server write" + msg); ctx.flush(); }
0
开源中国首席退堂鼓手
开源中国首席退堂鼓手
这个是server端口的代码
0
l
llIIll

这个Server端能找到吗?

返回顶部
顶部