/**
* @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 {
}
}
}