0
Nettyに構築しているサーバーから別のクライアントに接続しようとしています。 http://netty.io/4.1/xref/io/netty/example/proxy/package-summary.htmlchannelhandlerのnettyでクライアントに接続
だからChannelInboundHandlerAdapter
の私のサブクラスでは、私はこの
ctx.pipeline().addLast(new EchoTestHandler("localhost", 3030));
マイEchoTestHandlerがどのように見えるやろう:私はここに、プロキシの例を見て
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();
}
}
});
}
}
コンストラクタが呼び出されますが、まだ何も接続していないので、channelActive
は決して呼び出されません。あなたはchannelActive
コールを実行するためにプロキシサーバーに何かを接続する必要があり
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()
);
}
}
別のサーバーが稼動しています – Crystal