netty编写ngrok之HttpResponse编码(一)

5 篇文章 0 订阅

使用netty处理http请求的时候,一般都使用

HttpRequestDecoder HttpResponseEncoder

这个两个类,一个是解析解码request的一个是对response进行编码的

后来又出来一个类把这两个类的功能给替代了的类

HttpServerCodec

这个类包含请求的解码和响应的编码,是个二合一的功能类

在我们真正要处理的handler之前加上这个就可以出来上面解析请求的类了

像这样

            b.childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new HttpServerCodec());
                    ch.pipeline().addLast("serverHandler",new ServerHandler());
                }
            });

就可以对来的请求和出去的响应进行编解码的处理了,我的业务handler只需要处理逻辑,就不用考虑编解码的问题了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当然可以!下面是一个使用Spring Boot和Netty编写的简单群聊示例: 1. 首先,创建一个Spring Boot项目,并添加Netty和Spring Boot的依赖。 2. 创建一个Netty服务器类,用于处理客户端的连接和消息发送: ```java @Component public class ChatServer { private static final int PORT = 8080; private final ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); @PostConstruct public void start() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChatServerInitializer(channelGroup)); Channel channel = bootstrap.bind(PORT).sync().channel(); channel.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } } ``` 3. 创建一个Netty的ChannelInitializer类,用于配置ChannelPipeline: ```java public class ChatServerInitializer extends ChannelInitializer<SocketChannel> { private final ChannelGroup channelGroup; public ChatServerInitializer(ChannelGroup channelGroup) { this.channelGroup = channelGroup; } @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); pipeline.addLast(new StringDecoder()); pipeline.addLast(new StringEncoder()); pipeline.addLast(new ChatServerHandler(channelGroup)); } } ``` 4. 创建一个Netty的ChannelInboundHandlerAdapter类,用于处理接收到的消息并广播给所有连接的客户端: ```java public class ChatServerHandler extends ChannelInboundHandlerAdapter { private final ChannelGroup channelGroup; public ChatServerHandler(ChannelGroup channelGroup) { this.channelGroup = channelGroup; } @Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { Channel incoming = ctx.channel(); // 广播通知其他客户端有新用户加入 channelGroup.writeAndFlush("[SERVER] - " + incoming.remoteAddress() + " 加入\n"); channelGroup.add(incoming); } @Override public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { Channel outgoing = ctx.channel(); // 广播通知其他客户端有用户离开 channelGroup.writeAndFlush("[SERVER] - " + outgoing.remoteAddress() + " 离开\n"); channelGroup.remove(outgoing); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { Channel incoming = ctx.channel(); System.out.println("Client: " + incoming.remoteAddress() + " 上线"); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { Channel incoming = ctx.channel(); System.out.println("Client: " + incoming.remoteAddress() + " 掉线"); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Channel incoming = ctx.channel(); System.out.println("Client: " + incoming.remoteAddress() + " 发送消息:" + msg); // 广播消息给所有连接的客户端 channelGroup.writeAndFlush("[" + incoming.remoteAddress() + "]:" + msg + "\n"); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { Channel incoming = ctx.channel(); System.out.println("Client: " + incoming.remoteAddress() + " 异常"); cause.printStackTrace(); ctx.close(); } } ``` 5. 配置Spring Boot启动类: ```java @SpringBootApplication public class ChatApplication { public static void main(String[] args) { SpringApplication.run(ChatApplication.class, args); } } ``` 6. 运行Spring Boot应用程序,然后使用Telnet或其他工具连接到服务器的IP地址和端口号(默认为8080)。多个客户端连接到服务器后,发送的消息将被广播给所有连接的客户端。 这只是一个简单的群聊示例,你可以根据实际需求进行扩展和优化。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半块橘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值