我如何从服务器本身发送消息,而不是从 MessageHandlers 发送消息?我知道 InetSocketAddress,我从 MessageHandler 存储它,但我看不到任何直接使用套接字的方法。
例如:
public class QuoteOfTheMomentServer {
private final int port;
public QuoteOfTheMomentServer(int port) {
this.port = port;
}
Bootstrap b;
public void run() throws Exception {
b = new Bootstrap();
try {
b.group(new NioEventLoopGroup())
.channel(NioDatagramChannel.class)
.option(ChannelOption.SO_BROADCAST, true)
.handler(new QuoteOfTheMomentServerHandler());
b.bind(port).sync().channel().closeFuture().await();
} finally {
b.shutdown();
}
}
public static void main(String[] args) throws Exception {
int port;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
} else {
port = 8080;
}
new QuoteOfTheMomentServer(port).run();
}
}
我该如何添加方法
public void sendMessage(ByteBuf msg, InetSocketAddr addr) {
b.send(msg, addr);
}