【发布时间】:2020-01-08 01:43:20
【问题描述】:
我想保存一个传入通道 (ctx) 以供以后使用,例如在 HashMap 中。
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf in = (ByteBuf) msg;
System.out.println("Server received: " + in.toString(CharsetUtil.UTF_8));
// example channels.put("channelname", ctx);
}
我背后的想法是,我可以通过字符串从地图中获取 ctx,然后将来自另一个类的消息写入它,例如:
channels.get("keyforchannel").writeAndFlush("here my message to this specific channel");
有人知道我如何实现这一目标吗?
【问题讨论】: