服务器建立sslConnection后如何在netty客户端上获得回调
How to get callback on netty client after server establish sslConnection
我的服务器希望客户端连接到两个不同的套接字上,连接顺序很重要。客户端必须在第一个通道 ch1
上连接,并且在 SSL 握手之后服务器需要时间来创建用户会话。在处理程序中它看起来像这样:
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
log.debug("channelRegistered);
ctx.pipeline().get(SslHandler.class).handshakeFuture().addListener(
future -> initSession(ctx));
}
InitSession 方法创建内部对象来跟踪客户端。只有在 initSession 完成后,服务器才期望来自此客户端的第二个通道 ch2
上的连接。
我坚持编写客户端代码来执行此连接顺序。
天真的方法很简单:
public static void main(String[] args) throws Exception {
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
SslContext sslContext = provideSslContext();
Bootstrap b = new Bootstrap();
b.group(workerGroup)
.channel(NioSocketChannel.class)
.handler(new Channelinitializer(sslContext));
Channel ch1 = b.connect("localhost", 8008).sync().channel();
Thread.sleep(1000);
Bootstrap b1 = new Bootstrap();
b1.group(workerGroup)
.channel(NioSocketChannel.class)
.handler(new Channelinitializer(sslContext));
Channel ch2 = b1.connect("localhost", 8009).sync().channel();
}finally {
workerGroup.shutdownGracefully();
}
}
ch1
连接后,我们只需等待一段时间以确保服务器执行所需的所有操作。
强大的解决方案应该是什么样子?我可以使用任何回调来触发 ch2
连接吗?我正在使用 netty 4.0.36.Final
您可以只从管道中检索 SslHandler
并等待 handshakeFuture
或向其添加侦听器。然后完成后进行第二次连接。
类似于:
SslContext sslContext = provideSslContext();
Bootstrap b = new Bootstrap();
b.group(workerGroup)
.channel(NioSocketChannel.class)
.handler(new Channelinitializer(sslContext));
Channel ch1 = b.connect("localhost", 8008).sync().channel();
ch1.pipeline.get(SslHandler.class).handshakeFuture().sync()
Bootstrap b1 = new Bootstrap();
b1.group(workerGroup)
.channel(NioSocketChannel.class)
.handler(new Channelinitializer(sslContext));
Channel ch2 = b1.connect("localhost", 8009).sync().channel();
我的服务器希望客户端连接到两个不同的套接字上,连接顺序很重要。客户端必须在第一个通道 ch1
上连接,并且在 SSL 握手之后服务器需要时间来创建用户会话。在处理程序中它看起来像这样:
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
log.debug("channelRegistered);
ctx.pipeline().get(SslHandler.class).handshakeFuture().addListener(
future -> initSession(ctx));
}
InitSession 方法创建内部对象来跟踪客户端。只有在 initSession 完成后,服务器才期望来自此客户端的第二个通道 ch2
上的连接。
我坚持编写客户端代码来执行此连接顺序。 天真的方法很简单:
public static void main(String[] args) throws Exception {
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
SslContext sslContext = provideSslContext();
Bootstrap b = new Bootstrap();
b.group(workerGroup)
.channel(NioSocketChannel.class)
.handler(new Channelinitializer(sslContext));
Channel ch1 = b.connect("localhost", 8008).sync().channel();
Thread.sleep(1000);
Bootstrap b1 = new Bootstrap();
b1.group(workerGroup)
.channel(NioSocketChannel.class)
.handler(new Channelinitializer(sslContext));
Channel ch2 = b1.connect("localhost", 8009).sync().channel();
}finally {
workerGroup.shutdownGracefully();
}
}
ch1
连接后,我们只需等待一段时间以确保服务器执行所需的所有操作。
强大的解决方案应该是什么样子?我可以使用任何回调来触发 ch2
连接吗?我正在使用 netty 4.0.36.Final
您可以只从管道中检索 SslHandler
并等待 handshakeFuture
或向其添加侦听器。然后完成后进行第二次连接。
类似于:
SslContext sslContext = provideSslContext();
Bootstrap b = new Bootstrap();
b.group(workerGroup)
.channel(NioSocketChannel.class)
.handler(new Channelinitializer(sslContext));
Channel ch1 = b.connect("localhost", 8008).sync().channel();
ch1.pipeline.get(SslHandler.class).handshakeFuture().sync()
Bootstrap b1 = new Bootstrap();
b1.group(workerGroup)
.channel(NioSocketChannel.class)
.handler(new Channelinitializer(sslContext));
Channel ch2 = b1.connect("localhost", 8009).sync().channel();