使用Springboot + netty 打造聊天服务(一)
创始人
2024-12-14 16:32:53
0

目录

  • 1、创建Springboot工程
    • 1.1、项目创建
    • 1.2、环境配置
      • 1.2.1、检查settings.xml
      • 1.2.2、检查本地仓库
    • 1.3、项目结构配置
      • 1.3.1、配置JDK
    • 1.4、添加依赖
  • 2、构造NettyWebsocketServer
    • 2.1、创建一个 Netty 服务器配置类
    • 2.2、添加自定义handler
  • 3、使用ApiFox调试
    • 3.1、启动服务
    • 3.2、添加Apifox WebSocket接口
    • 3.3、客户端发送数据
    • 3.4、Debug调试数据
    • 3.5、客户端结果
    • 3.6、改造服务端代码
  • 4、总结

1、创建Springboot工程

在Idea里创建Springboot工程,Language选择Java,Type选择Maven,Project SDK 选择Java8。
在这里插入图片描述

1.1、项目创建

成功创建项目,Sringboot版本 2.7.6,Java版本8。
在这里插入图片描述

1.2、环境配置

检查Maven配置,主要检查settings.xml和本地Maven仓库
在这里插入图片描述

1.2.1、检查settings.xml

在settings.xml里配置上阿里云镜像

                  alimaven         central         aliyun maven         http://maven.aliyun.com/nexus/content/repositories/central/           

在这里插入图片描述

1.2.2、检查本地仓库

Maven版本3.6.3,在Maven文件夹下创建了res文件夹,做为Java工程的Maven本地仓库地址,此文件通常都在500MB以上,随着本地项目数而递增。
在这里插入图片描述

1.3、项目结构配置

打开【Project Structure】
在这里插入图片描述

1.3.1、配置JDK

修改SDK、Language level等配置。
在这里插入图片描述
在这里插入图片描述

1.4、添加依赖

上述操作都是打基础,搭架子和配置环境,下一步我们需要加载netty 的相关依赖包。
在 pom.xml 文件中添加 Netty 和 Spring Boot 相关的依赖:

              org.springframework.boot         spring-boot-starter                   io.netty         netty-all         4.1.68.Final         

2、构造NettyWebsocketServer

2.1、创建一个 Netty 服务器配置类

 import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; import org.springframework.context.annotation.Configuration; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpServerCodec; import io.netty.handler.stream.ChunkedWriteHandler; import io.netty.handler.timeout.IdleStateHandler; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.bootstrap.ServerBootstrap; import javax.annotation.PostConstruct; import lombok.extern.slf4j.Slf4j; import io.netty.channel.*;  @Slf4j @Configuration public class NettyWebsocketServer {      //bossGroup 连接线程组,主要负责接受客户端连接     EventLoopGroup bossGroup = new NioEventLoopGroup(1);     private EventLoopGroup workerGroup = new NioEventLoopGroup();     private ChannelFuture channelFuture;      @PostConstruct     public void start() throws InterruptedException {         try {             ServerBootstrap b = new ServerBootstrap();             b.group(bossGroup, workerGroup)                     .channel(NioServerSocketChannel.class)                     .option(ChannelOption.SO_BACKLOG, 128)                     .childOption(ChannelOption.SO_KEEPALIVE, true)                     .childHandler(new ChannelInitializer() {                         @Override                         protected void initChannel(SocketChannel socketChannel) throws Exception {                             ChannelPipeline pipeline = socketChannel.pipeline();                             // 因为使用 HTTP 协议,所以需要 HTTP编码器,解码器                             pipeline.addLast(new HttpServerCodec());                             // 以块方式,添加 chunkedWriter 处理器                             pipeline.addLast(new ChunkedWriteHandler());                             /**                              * 1. http数据在传输中是分段的,HttpObjectAggregator 可以把多个段聚合起来                              * 2. 这就是为什么当浏览器发送大量数据时,就会发出多次 http请求的原因                              */                             pipeline.addLast(new HttpObjectAggregator(8192));                             // 保存用户ip                             // pipeline.addLast(new HttpHeadersHandler());                             pipeline.addLast(new WebSocketServerProtocolHandler("/chat"));                              pipeline.addLast(new IdleStateHandler(60, 5, 0));                             pipeline.addLast(new ChatHandler());  //添加自定义handler                         }                     });              // Bind and start to accept incoming connections.             channelFuture = b.bind(9091).sync();             if(channelFuture.isSuccess()) {                 log.info("netty启动成功");             }             // 对通道关闭进行监听             channelFuture.channel().closeFuture().sync();         } finally {             if (channelFuture != null && channelFuture.isSuccess()) {                 System.out.println("Netty server started on port 9091");             } else {                 System.err.println("Netty server failed to start");             }         }     } } 

2.2、添加自定义handler

创建一个自定义的 ChannelHandler 来处理客户端的请求:

import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter;  public class ChatHandler extends ChannelInboundHandlerAdapter {      @Override     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {         // 处理收到的消息         System.out.println("Received message: " + msg);         ctx.writeAndFlush("Message received: " + msg);     }      @Override     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {         cause.printStackTrace();         ctx.close();     } } 

3、使用ApiFox调试

3.1、启动服务

在这里插入图片描述

3.2、添加Apifox WebSocket接口

在这里插入图片描述
输入ws接口地址,成功连接上netty服务
在这里插入图片描述

3.3、客户端发送数据

在这里插入图片描述

3.4、Debug调试数据

这里可以看到已经接收到数据了。

我们在Debug数据时,可以看到msg的data类型
TextWebSocketFrame(data: PooledUnsafeDirectByteBuf(ridx: 0, widx: 61, cap: 61))

在这里插入图片描述

3.5、客户端结果

客户端收不到服务端回复的消息。
在这里插入图片描述

3.6、改造服务端代码

在这里插入图片描述
查看数据结果
在这里插入图片描述

4、总结

在文章里,我们实现了Springboot 集成netty,并使用apiFox客户端发送了消息到netty服务,最终客户端收到服务端的应答。
下一章节,我们讲解解码器的使用,使得我们的数据可以可视化,以及正常的展示。

相关内容

热门资讯

外挂要领!uupoker有透视... 外挂要领!uupoker有透视吗,werplan外开挂-确实真的有辅助攻略(哔哩哔哩)1、进入到uu...
透视脚本!wepoker游戏安... 透视脚本!wepoker游戏安装教程(透视)切实是真的辅助软件(有挂工具)-哔哩哔哩1、wepoke...
终于知道!智星菠萝透视,新51... 终于知道!智星菠萝透视,新518互游插件,好像是真的有辅助安装(有挂教学)1、打开软件启动之后找到中...
外挂策略!佛手在线大菠萝辅助,... 外挂策略!佛手在线大菠萝辅助,pokerrrr2辅助-好像有辅助工具(哔哩哔哩)1、在佛手在线大菠萝...
据文件显示!wepoker看底... 据文件显示!wepoker看底牌(透视)本来真的是有辅助神器(有挂头条)-哔哩哔哩1、点击下载安装,...
玩家必知教程!黑侠破解wepo... 玩家必知教程!黑侠破解wepoker,GG大玩家辅助软件,切实是有辅助教程(有挂攻略)GG大玩家辅助...
外挂模块!菠萝德州透视脚本,p... 外挂模块!菠萝德州透视脚本,poker master辅助-一贯真的有辅助脚本(哔哩哔哩)运菠萝德州透...
随着!wepoker数据分析工... 随着!wepoker数据分析工具(透视)切实是真的辅助修改器(揭秘有挂)-哔哩哔哩1、wepoker...
分享一款!aapoker怎么控... 分享一款!aapoker怎么控制牌,微信超级3+1辅助,真是存在有辅助辅助(有挂助手)1、点击下载安...
外挂手筋!大菠萝免费辅助器,竞... 外挂手筋!大菠萝免费辅助器,竞技联盟透视插件-都是真的有辅助教程(哔哩哔哩)1、不需要AI权限,帮助...