netty服务器怎么返回响应,从管道中删除httpObjectAggregator时,netty服务器未返回响应...

我正在尝试使用netty 4.1.16.final创建简单的http服务器。

下面是HTTP服务器的代码-

EventLoopGroup masterGroup = new NioEventLoopGroup();

EventLoopGroup slaveGroup = new NioEventLoopGroup();

final ServerBootstrap bootstrap =

new ServerBootstrap()

.group(masterGroup, slaveGroup)

.channel(NioServerSocketChannel.class)

.childHandler(new ChannelInitializer() {

@Override

protected void initChannel(SocketChannel ch) {

ch.pipeline().addLast("codec", new HttpServerCodec());

ch.pipeline().addLast("aggregator",

new HttpObjectAggregator(512 * 1024));

ch.pipeline().addLast("request",

new HTTPSimpleChannelInboundHandler());

}

}).option(ChannelOption.SO_BACKLOG, 128)

.childOption(ChannelOption.SO_KEEPALIVE, true);

channel = bootstrap.bind(8080).sync();

HTTP处理程序类的代码

HTTPSimpleChannelInboundHandler

低于-

public class HTTPSimpleChannelInboundHandler extends SimpleChannelInboundHandler {

protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {

HttpResponseStatus responseStatus = OK;

FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, responseStatus, Unpooled.copiedBuffer("My Netty".getBytes()));

response.headers().add(request.headers());

response.headers().set(CONTENT_LENGTH, response.content().readableBytes());

if (isKeepAlive(request)) {

response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);

}

if (is100ContinueExpected(request)) {

ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));

}

ctx.writeAndFlush(response);

}

public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {

cause.printStackTrace();

ctx.close();

}

}

上面的代码工作正常。

但当我在服务器代码中注释下面的行时,没有得到响应。

ch.pipeline().addLast("aggregator", new HttpObjectAggregator(512 * 1024));

下面是我从服务器上得到的日志-

01:37:14.806 [nioEventLoopGroup-3-2] DEBUG io.netty.channel.DefaultChannelPipeline - Discarded inbound message DefaultHttpRequest(decodeResult: success, version: HTTP/1.1)

GET /test HTTP/1.1

Host: localhost:5055

User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:60.0) Gecko/20100101 Firefox/60.0

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-US,en;q=0.5

Accept-Encoding: gzip, deflate

Cookie: _ga=GA1.1.177481759.1523295602; Idea-46064427=2276f52b-2928-4410-8f4c-c7561bd33457

Connection: keep-alive

Upgrade-Insecure-Requests: 1 that reached at the tail of the pipeline. Please check your pipeline configuration.

01:37:14.806 [nioEventLoopGroup-3-2] DEBUG io.netty.channel.DefaultChannelPipeline - Discarded inbound message EmptyLastHttpContent that reached at the tail of the pipeline. Please check your pipeline configuration.

为什么httpobjectaggregator在channelpipeline中是必需的?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值