简体   繁体   English

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

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

I am trying to close a TCP connection from netty reactor.ipc.netty.tcp.TcpClient, but i can't find a way to do it easily, there is no "disconnect", "stop" or "close" method. 我正在尝试从netty Reactor.ipc.netty.tcp.TcpClient关闭TCP连接,但是我找不到轻松实现此目的的方法,没有“断开连接”,“停止”或“关闭”方法。 Can anyone help me? 谁能帮我? I am using reactor-netty.0.7.9.RELEASE library. 我正在使用Reactor-netty.0.7.9.RELEASE库。

My class is structured as follows: 我的课程结构如下:

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?
}

I appreciate your help, thank you very much in advance. 感谢您的帮助,非常感谢。

Yes, sorry I didn't post it before. 是的,抱歉,我之前没有发布过。 All I had to do was to pass the connection from tcp client to a field variable, so I was able to dispose the connection using the dispose method. 我要做的就是将tcp客户端的连接传递给字段变量,因此我可以使用dispose方法来处理连接。 Here is the code. 这是代码。

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