[英]Netty IO send Message to Client
how can I send a message from the Server to the Client using Netty? 如何使用Netty从服务器向客户端发送消息? I know you can do it like
我知道你可以做到
for (Channel c : channels1) {
c.writeAndFlush("TEXT \r\n");
}
but it is only usable from inside the ChatServerHandler Class. 但是它只能在ChatServerHandler类内部使用。 Is there a method to send Messages to the Client or how can I make a method which I can call like for example
有没有一种方法可以将消息发送到客户端,或者如何制作可以调用的方法,例如
sendMessageToServer("MESSAGE");
void sendMessageToServer(String message) {
final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
for (Channel c : channels) {
c.writeAndFlush("TEXT \r\n");
}
}
? ? If I get the Channels like in ChatServerHandler using
如果我使用ChatServerHandler中的频道
final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
I get a Channel size of 0. 我得到的通道大小为0。
final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
This constructs a new ChannelGroup
with nothing inside. 这将构造一个没有任何内容的新
ChannelGroup
。
You should have a class extends SimpleChannelInboundHandler<?>
. 您应该具有一个扩展
SimpleChannelInboundHandler<?>
。
The channelActive(ChannelHandlerContext chc)
method is called when something is connected. 连接某些对象时,将调用
channelActive(ChannelHandlerContext chc)
方法。 Simply add Channel
instance to your channels
by channels.add(chc.channel());
只需添加
Channel
实例您的channels
由channels.add(chc.channel());
You may have to make your ChannelGroup
public so it can be acessed everywhere. 您可能必须将
ChannelGroup
公开,以便可以在任何地方使用。 Good luck. 祝好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.