繁体   English   中英

如何使用Netty Reactor关闭TcpClient连接?

[英]How can I close a TcpClient connection using Netty Reactor?

我正在尝试从netty Reactor.ipc.netty.tcp.TcpClient关闭TCP连接,但是我找不到轻松实现此目的的方法,没有“断开连接”,“停止”或“关闭”方法。 谁能帮我? 我正在使用Reactor-netty.0.7.9.RELEASE库。

我的课程结构如下:

private TcpClient client;
private NettyOutbound out;

public Mono<? extends NettyContext> connect() {
    client = TcpClient.create(host, port);
    return client.newHandler(this::handleConnection)
            .log("newHandler");
}

private Publisher<Void> handleConnection(NettyInbound in, NettyOutbound out) {
    this.out = out;
    return out
            .neverComplete()    //keep connection alive
            .log("Never close");
}

public void disconnect() {
    client = TcpClient. //What can i put here to close the connection?
}

感谢您的帮助,非常感谢。

是的,抱歉,我之前没有发布过。 我要做的就是将tcp客户端的连接传递给字段变量,因此我可以使用dispose方法来处理连接。 这是代码。

private NettyOutbound out;
private Connection connection;

public ServerResponse connect() {
    return TcpClient.create()
        .host(tcpConfig.getHost())
        .port(tcpConfig.getPort())
        .handle(this::handleConnection)
        .connect()
        .flatMap(connection -> {
            this.connection = connection;
            log.info("Sending response to http client.");
            return ServerResponse.ok().build();
        });
}

private Publisher<Void> handleConnection(NettyInbound in, NettyOutbound out) {
    this.out = out;
    in.receive().asString(Charsets.ISO_8859_1)
        .log("In received")
        .subscribe(frameStr -> log.info(frameStr));
    return out
        .neverComplete()    //keep connection alive
        .log("Never close");
}

public void disconnect() {
    this.connection.dispose();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM