admin管理员组文章数量:1404050
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?
本文标签:
版权声明:本文标题:When writing an HTTP Server in Netty, do I have to implement the logic of whether the Channel is closed or not - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744219336a2595788.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论