java Netty - 如何获取所有客户端频道?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10372801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:45:14  来源:igfitidea点击:

Netty - how to get all client channel?

javanetworkingnetty

提问by chentingpc

I was using netty example codes - telnet packet, Now the code can establish server and client to chat using telnet, but client can only talk to server. I am rewriting it to make the clients can talk to all the clients, so I need to keep a channel list, so when a client is contact the server, the server can send the message to all of the clients. Can anyone tell me how could I get all clients channel? (The example code is enter link description here)

我使用的是netty示例代码- telnet数据包,现在代码可以建立服务器和客户端使用telnet聊天,但客户端只能与服务器通话。我正在重写它以使客户端可以与所有客户端通话,因此我需要保留一个频道列表,以便当客户端与服务器联系时,服务器可以将消息发送给所有客户端。谁能告诉我如何获得所有客户渠道?(示例代码在此处输入链接描述

回答by HCarrasko

For Netty 4.0.X

对于 Netty 4.0.X

In main Class you need to declare the ChannelGroup object:

在主类中,您需要声明 ChannelGroup 对象:

 final ChannelGroup channels = 
                new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

When a new client is connected (you should pass the channels object in the constructor to you handler class):

连接新客户端时(您应该将构造函数中的通道对象传递给处理程序类):

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    channels.add(ctx.channel());
}

To get all clients, just iterate the channels object:

要获取所有客户端,只需迭代通道对象:

for (Channel ch : channels) {
    //do something with ch object
}

Hope it helps.

希望能帮助到你。

回答by Nicholas

Mauricio's proposal is a good one. In addition, the Netty API already provides a channel container in the ChannelGroup. It is thread safe and also provides several additional features such as group operations on all contained channels and automatic removal of contained channels when they are closed. From the javadoc:

毛里西奥的提议很好。此外,Netty API 已经在ChannelGroup 中提供了一个通道容器。它是线程安全的,还提供了一些附加功能,例如对所有包含的通道进行组操作以及在关闭时自动删除包含的通道。从javadoc:

A thread-safe Set that contains open Channels and provides various bulk operations on them. Using ChannelGroup, you can categorize Channels into a meaningful group (e.g. on a per-service or per-state basis.) A closed Channel is automatically removed from the collection, so that you don't need to worry about the life cycle of the added Channel. A Channel can belong to more than one ChannelGroup.

一个线程安全的 Set,包含开放的 Channels 并提供对它们的各种批量操作。使用 ChannelGroup,您可以将 Channels 分类为一个有意义的组(例如基于每个服务或每个状态。)关闭的 Channel 会自动从集合中删除,因此您无需担心通道的生命周期添加了频道。一个频道可以属于多个频道组。

回答by Jonas Adler

here's a little example (overrides channelConnected of SimpleChannelUpstreamHandler):

这是一个小例子(覆盖 SimpleChannelUpstreamHandler 的 channelConnected):

ChannelGroup allConnected = new DefaultChannelGroup("all-connected");

@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    super.channelConnected(ctx, e);
    allConnected.add(e.getChannel());
}

you can now send messages to all connected channels like so:

您现在可以向所有连接的频道发送消息,如下所示:

    ChannelBuffer cb = ChannelBuffers.wrappedBuffer("hello".getBytes(Charset.forName("UTF-8")));
    allConnected.write(cb);

回答by Maurício Linhares

At the channelConnectedevent, grab the client from the ChannelHandlerContextand store it somewhere (a concurrent collection would be nice, like ConcurrentHashMap). You will also have to implement the channelClosedmethod to remove the disconnected channel from your collection.

channelConnected事件中,从ChannelHandlerContext获取客户端并将其存储在某处(并发集合会很好,比如ConcurrentHashMap)。您还必须实现channelClosed方法以从您的集合中删除断开连接的频道。