<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>archive</title><link>https://archive-w.netlify.app/doc/framework/netty/lesson/</link><description>Recent content on archive</description><generator>Hugo</generator><language>zh-CN</language><atom:link href="https://archive-w.netlify.app/doc/framework/netty/lesson/index.xml" rel="self" type="application/rss+xml"/><item><title/><link>https://archive-w.netlify.app/doc/framework/netty/lesson/01_netty-code-flow/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://archive-w.netlify.app/doc/framework/netty/lesson/01_netty-code-flow/</guid><description>&lt;h2 id="netty编码流程">
 Netty编码流程
 &lt;a class="anchor" href="#netty%e7%bc%96%e7%a0%81%e6%b5%81%e7%a8%8b">#&lt;/a>
&lt;/h2>
&lt;p class="warn">写一个简单的http server&lt;/p>
&lt;div class="outer yosemite">&lt;div class="dot red">&lt;/div>&lt;div class="dot amber">&lt;/div>&lt;div class="dot green">&lt;/div>&lt;/div>
&lt;div class="code-toolbar">&lt;pre data-lang="java" data-line="" class="language-java line-numbers" style="max-height: none">&lt;code class="language-java">public class TestServer {
 public static void main(String[] args) {
 EventLoopGroup bossGroup = new NioEventLoopGroup();
 EventLoopGroup wordGroup = new NioEventLoopGroup();

 ServerBootstrap serverBootstrap = new ServerBootstrap();

 try {
 serverBootstrap.group(bossGroup,wordGroup)
 .channel(NioServerSocketChannel.class)
 
 // channel初始化器
 .childHandler(new ChannelInitializer&amp;lt;SocketChannel&amp;gt;() {
 @Override
 protected void initChannel(SocketChannel socketChannel) throws Exception {
 ChannelPipeline pipeline = socketChannel.pipeline();
 pipeline.addLast(&amp;quot;httpServerCodec&amp;quot;,new HttpServerCodec());
 
 // 在pipeline中加入自定义的解码器 handler
 pipeline.addLast(&amp;quot;httpServerContent&amp;quot;, new SimpleChannelInboundHandler&amp;lt;HttpObject&amp;gt;() {
 @Override
 protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject msg) throws Exception {
 /* 
 另外有一个 LastHttpContent 类型的 EMPTY_LAST_CONTENT 对象
 来源：HttpServerCodec 中 inboundHandler 为 HttpServerRequestDecoder ==&amp;gt; HttpObjectDecoder
 channelRead 的时候会进行解码，追加两个对象；
 out.add(message);
 out.add(LastHttpContent.EMPTY_LAST_CONTENT);
 所以追加if判断
 */
 if(msg instanceof HttpRequest){
 ByteBuf byteBuf = Unpooled.copiedBuffer(&amp;quot;hello world&amp;quot;, CharsetUtil.UTF_8);
 FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.OK,byteBuf);
 response.headers().set(HttpHeaderNames.CONTENT_TYPE,&amp;quot;text/plain&amp;quot;);
 response.headers().set(HttpHeaderNames.CONTENT_LENGTH,byteBuf.readableBytes());
 response.headers().set(HttpHeaderNames.SERVER,&amp;quot;X-eli&amp;quot;);

 channelHandlerContext.writeAndFlush(response);
 }
 }
 });
 }
 });
 ChannelFuture channelFuture = serverBootstrap.bind(13308).sync();
 channelFuture.channel().closeFuture().sync();
 }catch (Exception e){
 e.printStackTrace();
 }finally {
 bossGroup.shutdownGracefully();
 wordGroup.shutdownGracefully();
 }
 }
}
&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="reference">
 Reference
 &lt;a class="anchor" href="#reference">#&lt;/a>
&lt;/h2>
&lt;ul>
&lt;li>


 &lt;a href="https://www.bilibili.com/video/BV1c4411J7Ty?p=5" rel="noopener" target="_blank">https://www.bilibili.com/video/BV1c4411J7Ty?p=5&lt;/a>&lt;/li>
&lt;/ul></description></item></channel></rss>