Ich versuche, eine Verbindung zu einem anderen Client von einem Server, den ich in Netty erstellen. Ich schaute auf das Proxy Beispiel hier: http://netty.io/4.1/xref/io/netty/example/proxy/package-summary.htmlVerbindung zum Client in Netty in Channelhandler
Also in meiner Unterklasse von ChannelInboundHandlerAdapter
, versuche ich diesen
ctx.pipeline().addLast(new EchoTestHandler("localhost", 3030));
Mein EchoTestHandler zu tun wie folgt aussieht:
public class EchoTestHandler extends ChannelInboundHandlerAdapter {
private final String host;
private final int port;
private Channel outboundChannel;
public EchoTestHandler(String host, int port) {
System.out.println("constructor echo test handler");
this.host = host;
this.port = port;
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
System.out.println("channel test handler");
final Channel inboundChannel = ctx.channel();
// start the connection attempt
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(inboundChannel.eventLoop())
.channel(ctx.channel().getClass())
.handler(new CryptoServerHandler(inboundChannel));
ChannelFuture future = bootstrap.connect(host, port);
outboundChannel = future.channel();
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) {
if (channelFuture.isSuccess()) {
// connection complete, start to read first data
inboundChannel.read();
} else {
// close the connection if connection attempt has failed
inboundChannel.close();
}
}
});
}
}
Der Konstruktor aufgerufen wird, aber Da es noch nichts verbindet, wird channelActive
nie aufgerufen. Ich habe auch versucht dies, mehr ähnlich dem Proxy-Beispiel:
ctx.pipeline().addLast(new EchoServerInitializer("localhost", 3020));
Und dann EchoServerInitializer:
public class EchoServerInitializer extends ChannelInitializer<SocketChannel> {
private final String host;
private final int port;
public EchoServerInitializer(String host, int port) {
System.out.println("constructor EchoServerInitializer");
this.host = host;
this.port = port;
}
@Override
public void initChannel(SocketChannel ch) {
System.out.println("EchoServerInitializer initChannel");
ch.pipeline().addLast(
new LoggingHandler(LogLevel.INFO),
new EchoServerHandler()
);
}
}
Ich habe einen anderen Server in Betrieb – Crystal