When writing an HTTP Server in Netty, do I have to implement the logic of whether the Channel is closed or not - Stack Overflow

I'm new to Netty and I looked at part of the source code using ioty.example.http.file.HttpStaticFi

I'm new to Netty and I looked at part of the source code using ioty.example.http.file.HttpStaticFileServer as a starting point. I couldn't find any logic to control whether a Channel is closed or not.

I found that in the ioty.example.http.file.HttpStaticFileServerHandler#sendAndCleanupConnection(ChannelHandlerContext ctx, FullHttpResponse response) method the ChannelFutureListener.CLOSE callback is added by checking if it is a keepAlive.

/**
 * If Keep-Alive is disabled, attaches "Connection: close" header to the response
 * and closes the connection after the response being sent.
 */
private void sendAndCleanupConnection(ChannelHandlerContext ctx, FullHttpResponse response) {
    final FullHttpRequest request = this.request;
    final boolean keepAlive = HttpUtil.isKeepAlive(request);
    HttpUtil.setContentLength(response, response.content().readableBytes());
    if (!keepAlive) {
        // We're going to close the connection as soon as the response is sent,
        // so we should also make it clear for the client.
        response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
    } else if (request.protocolVersion().equals(HTTP_1_0)) {
        response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }

    ChannelFuture flushPromise = ctx.writeAndFlush(response);

    if (!keepAlive) {
        // Close the connection as soon as the response is sent.
        flushPromise.addListener(ChannelFutureListener.CLOSE);
    }
}

I wonder, when writing an HTTP Server in Netty, do I have to implement the logic of whether the Channel is closed or not?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744219337a4563704.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信