Netty 是一个利用 Java 的高级网络的能力,隐藏其背后的复杂性而提供一个易于使用的 API 的客户端/服务器框架。
Maven依赖
SpringBootApplication
启动器中需要new一个NettyServer,并显式调用启动netty。
@SpringBootApplication publicclassSpringCloudStudyDemoApplication{ publicstaticvoidmain(String[]args){ SpringApplication.run(SpringCloudStudyDemoApplication.class,args); try{ newNettyServer(12345).start(); System.out.println("https://blog.csdn.net/moshowgame"); System.out.println("http://127.0.0.1:6688/netty-websocket/index"); }catch(Exceptione){ System.out.println("NettyServerError:"+e.getMessage()); } } }
NettyServer
启动的NettyServer,这里进行配置
/** *NettyServerNetty服务器配置 *@authorzhengkai.blog.csdn.net *@date2019-06-12 */ publicclassNettyServer{ privatefinalintport; publicNettyServer(intport){ this.port=port; } publicvoidstart()throwsException{ EventLoopGroupbossGroup=newNioEventLoopGroup(); EventLoopGroupgroup=newNioEventLoopGroup(); try{ ServerBootstrapsb=newServerBootstrap(); sb.option(ChannelOption.SO_BACKLOG,1024); sb.group(group,bossGroup)//绑定线程池 .channel(NioServerSocketChannel.class)//指定使用的channel .localAddress(this.port)//绑定监听端口 .childHandler(newChannelInitializer
MyChannelHandlerPool
通道组池,管理所有websocket连接
/** *MyChannelHandlerPool *通道组池,管理所有websocket连接 *@authorzhengkai.blog.csdn.net *@date2019-06-12 */ publicclassMyChannelHandlerPool{ publicMyChannelHandlerPool(){} publicstaticChannelGroupchannelGroup=newDefaultChannelGroup(GlobalEventExecutor.INSTANCE); }
MyWebSocketHandler
处理ws一下几种情况:
channelActive与客户端建立连接
channelInactive与客户端断开连接
channelRead0客户端发送消息处理
/** *NettyServerNetty服务器配置 *@authorzhengkai.blog.csdn.net *@date2019-06-12 */ publicclassNettyServer{ privatefinalintport; publicNettyServer(intport){ this.port=port; } publicvoidstart()throwsException{ EventLoopGroupbossGroup=newNioEventLoopGroup(); EventLoopGroupgroup=newNioEventLoopGroup(); try{ ServerBootstrapsb=newServerBootstrap(); sb.option(ChannelOption.SO_BACKLOG,1024); sb.group(group,bossGroup)//绑定线程池 .channel(NioServerSocketChannel.class)//指定使用的channel .localAddress(this.port)//绑定监听端口 .childHandler(newChannelInitializer
socket.html
主要是连接ws,发送消息,以及消息反馈
-
服务器
+关注
关注
12文章
9361浏览量
86255 -
API
+关注
关注
2文章
1524浏览量
62526 -
网络
+关注
关注
14文章
7607浏览量
89389
原文标题:Spring Boot + Netty + WebSocket 实现消息推送
文章出处:【微信号:AndroidPush,微信公众号:Android编程精选】欢迎添加关注!文章转载请注明出处。
发布评论请先 登录
相关推荐
基于多路复用模型的Netty框架
基于阿里云移动推送的移动应用推送模式最佳实践
如何实现服务器自动推送消息?
怎么去理解netty
怎样使用springboot整合netty来开发一套高性能的通信系统呢
Springboot整合netty框架实现终端、通讯板子(单片机)TCP/UDP通信案例

Netty如何做到单机百万并发?
netty推送消息接口及实现
jdk17下netty导致堆内存疯涨原因排查

评论