Netty学习笔记之:Http编码和解码

本文介绍了Netty中处理Http和WebSocket的编码解码过程。针对Http,Netty提供了HttpClientCodec和HttpServerCodec,配合HttpObjectAggregator进行消息聚合。对于Https,通过SslContext创建SSLEngine并添加SslHandler。在WebSocket服务端,使用WebSocketServerProtocolHandler处理升级握手,并定义TextFrameHandler、BinaryFrameHandler和ContinuationFrameHandler分别处理不同类型的WebSocket帧。
private final boolean client;

/**
* @param client
*/
public HttpPipelindeInitializer(boolean client) {
this.client = client;
}


@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
if (client) {
pipeline.addLast("decoder",new HttpResponseDecoder());//如果是客户端,则添加HttpResponseDecoder以处理来自服务器的响应!
pipeline.addLast("encoder",new HttpResponseEncoder());//如果是客户端,则添加HttpRequestEncoder以向服务器发送请求。
} else {
pipeline.addLast("decoder",new HttpRequestDecoder());//如果是服务器,则添加HttpRequestEncoder以向客户端发送响应。
pipeline.addLast("encoder",new HttpRequestEncoder());//如果是服务器,则添加HttpRequestDecoder以接受来自客户端的请求
}

}


Http聚合

public class HttpAggregatorInitializer extends ChannelInitializer<Channel> {
private final boolean isClient;

/**
* @param isClient
*/
public HttpAggregatorInitializer(boolean isClient) {
this.isClient = isClient;
}

@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
/**
* Netty提供了一个聚合器,可以将多个消息部分合并为 FullHttpRequest或者FullHttpResponse消息。
* 通过这样的方式,可以总是看到完整的消息内容。

* 由于消息分段需要被缓冲,直到可以转发一个完整的消息给下一个ChannelInboundHandler。
* 这个操作有轻微的开销。其所带来的好处便是不用关心消息碎片了
*/
if (isClient) {//是客户端,添加HTTPClientCodec
pipeline.addLast("codec",new HttpClientCodec());
} else {//服务器,添加HttpServerCodec
pipeline.addLast("codec",new HttpServerCodec());
}
pipeline.addLast("aggregator",
new HttpObjectAggregator(512 * 1024));//将最大的消息大小为512Kb的HttpObjectAggregator添加到ChanelPipeline
}


}

Https

public class HttpsCodecInitializer extends ChannelInitializer<Channel> {


private final SslContext context;
private final boolean isClient;

/**
* @param context
* @param isClient
*/
public HttpsCodecInitializer(SslContext context, boolean isClient) {
this.context = context;
this.isClient = isClient;
}





@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
SSLEngine engine = context.newEngine(ch.alloc());
pipeline.addLast("ssl",new SslHandler(engine));//将SSLHandler添加到ChannelPipeline中以shiyongHttps

if (isClient) {
pipeline.addLast("codec",new HttpClientCodec());
} else {
pipeline.addLast("codec", new HttpServerCodec());
}
}

}


在服务端支持Websocket

public class WebSocketServerInitalizer extends ChannelInitializer<Channel> {


@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(
new HttpServerCodec(),
new HttpObjectAggregator(65536),//为握手提供聚合的HttpRequest
new WebSocketServerProtocolHandler("/websocket"),//被请求的端点是"/websocket",则处理该升级握手
new TextFrameHandler(),//TextFrameHandler处理TextWebSocketFrame
new BinaryFrameHandler(),//BinaryFrameHandler处理BinaryWebScoketFrame
new ContinuationFrameHandler());//同上
}
public static final class TextFrameHandler extends SimpleChannelInboundHandler<TextWebSocketFrame>{
@Override
protected void messageReceived(ChannelHandlerContext ctx,TextWebSocketFrame msg) throws Exception {
//Handle text frame;
}
}
public static final class BinaryFrameHandler extends SimpleChannelInboundHandler<BinaryWebSocketFrame>{
@Override
protected void messageReceived(ChannelHandlerContext ctx,BinaryWebSocketFrame msg) throws Exception {
//Handle binary frame;
}
}
public static final class ContinuationFrameHandler extends SimpleChannelInboundHandler<ContinuationWebSocketFrame>{
@Override
protected void messageReceived(ChannelHandlerContext ctx,ContinuationWebSocketFrame msg) throws Exception {
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值