0

我正在尝试关闭来自 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?
}

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

4

1 回答 1

0

是的,抱歉我之前没有发过。我所要做的就是将连接从 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();
}
于 2018-12-10T17:39:02.793 回答