【问题标题】:netty: save channel handler context for later usenetty:保存通道处理程序上下文以供以后使用
【发布时间】: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");

有人知道我如何实现这一目标吗?

【问题讨论】:

    标签: java netty


    【解决方案1】:

    Netty 为此提供了ChannelGroup。 它不是地图,而是用于索引活动通道的线程安全集。 它具有许多有用的功能,例如写入组中的所有频道、使用ChannelMatcher 过滤您要写入的频道以及在频道关闭时自动弹出频道。

    【讨论】:

      猜你喜欢
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      • 2015-07-07
      • 1970-01-01
      相关资源
      最近更新 更多