1. 程式人生 > >Netty學習筆記之:Http編碼和解碼

Netty學習筆記之:Http編碼和解碼

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 {
}
}
}