netty示例-http协议传输

  1. 1.HttpServer.java
  2. 2.TestHttpServerInitlizer.java
  3. 3.TestHttpServerHandler.java
  4. 4.HttpHandler拦截器解答

1.HttpServer.java

public class TestHttpServer {
    public static void main(String[] args){
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workGroup = new NioEventLoopGroup();
        try{
            ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(bossGroup,workGroup).channel(NioServerSocketChannel.class).childHandler(new TestHttpServerInitlizer());
            ChannelFuture channelFuture = serverBootstrap.bind(8081).sync();
            System.out.println("服务器正在服务。。。");
            channelFuture.channel().closeFuture().sync();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            workGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }
}

2.TestHttpServerInitlizer.java

public class TestHttpServerInitlizer extends ChannelInitializer<SocketChannel>{
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        pipeline.addLast("httpServerCodec",new HttpServerCodec());
        pipeline.addLast("testServerHandler",new TestHttpServerHandler());
    }    
}

3.TestHttpServerHandler.java

public class TestHttpServerHandler extends     SimpleChannelInboundHandler<HttpObject>{
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject msg) throws Exception {
        System.out.println(msg.getClass());
        if(msg instanceof HttpRequest){
            HttpRequest request = (HttpRequest)msg;
            System.out.println("请求方式"+request.method().name());
            URI uri = new URI(request.uri());
            if("/favicon.ico".equals(uri.getPath())){
                System.out.println("favicon.ico");
                return;
            }
            ByteBuf contxt = Unpooled.copiedBuffer("hello word",CharsetUtil.UTF_8);
            FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,contxt);
            System.out.println(response);
            response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain");
            response.headers().set(HttpHeaderNames.CONTENT_LENGTH,contxt.readableBytes());
            channelHandlerContext.writeAndFlush(response);
            channelHandlerContext.channel().close();//此时才算关闭
        }
    }
    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("registered");
        super.channelRegistered(ctx);
    }
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("active");
        super.channelActive(ctx);
    }
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("inactive");
        super.channelInactive(ctx);
    }
    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("unregistered");
        super.channelUnregistered(ctx);
    }
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("handlerAdded");
        super.handlerAdded(ctx);
    }
}

4.HttpHandler拦截器解答

  Netty自带的Http编解码组件HttpServerCodec对通信数据进行编解码。

文章标题:netty示例-http协议传输

发布时间:2020-01-17, 09:52:45

最后更新:2020-01-17, 09:52:46