作者:小傅哥
博客:https://bugstack.cn
沉淀、分享、成长,让自己和他人都能有所收获!前言介绍
本章节我们模仿微信聊天页面,开发一个基.NETty搭建WebSocket通信案例。Netty的应用方面非常广;聊天、MQ、RPC、数据等等,在5G到来的时候更加需要大量数据传输,Netty的应用也会更加广阔。
itstack-demo-netty-2-05 └── src ├── mAIn │ ├── JAVA │ │ └── org.itstack.demo.netty │ │ ├── domain │ │ │ ├── ClientMsgProtocol.java │ │ │ └── ServerMsgProtocol.java │ │ ├── server │ │ │ ├── MyChannelInitializer.java │ │ │ ├── MyServerHandler.java │ │ │ └── NettyServer.java │ │ ├── util │ │ │ ├── ChannelHandler.java │ │ │ └── MsgUtil.java │ │ └── web │ │ ├── NettyApplication.java │ │ └── NettyController.java │ └── resources │ │ └── application.yml │ └── webapp │ ├── res │ └── WEB-INF │ └── index.jsp │ └── test └── java └── org.itstack.demo.test ├── ApiTest.java ├── NettyClientTest.java └── NettyServerTest.java
演示部分重点代码块,完整代码下载关注公众号;bugstack虫洞栈
server/MyChannelInitializer.java *websocket处理协议
/** * 消息传输协议 * 虫洞栈:https://bugstack.cn * Create by fuzhengwei on @2019 */ public class MyChannelInitializer extends ChannelInitializer { @Override protected void initChannel(SocketChannel channel) { channel.pipeline().addLast("http-codec", new HttpServerCodec()); channel.pipeline().addLast("aggregator", new HttpObjectAggregator(65536)); channel.pipeline().addLast("http-chunked", new ChunkedWriteHandler()); // 在管道中添加我们自己的接收数据实现方法 channel.pipeline().addLast(new MyServerHandler()); } }
server/MyServerHandler.java *处理websocket消息信息
/** * 消息传输协议 * 虫洞栈:https://bugstack.cn * Create by fuzhengwei on @2019 */ public class MyServerHandler extends ChannelInboundHandlerAdapter { private Logger logger = LoggerFactory.getLogger(MyServerHandler.class); private WebSocketServerHandshaker handshaker; /** * 当客户端主动链接服务端的链接后,这个通道就是活跃的了。也就是客户端与服务端建立了通信通道并且可以传输数据 */ @Override public void channelActive(ChannelHandlerContext ctx) throws exception { SocketChannel channel = (SocketChannel) ctx.channel(); logger.info("链接报告开始"); logger.info("链接报告信息:有一客户端链接到本服务端"); logger.info("链接报告IP:{}", channel.localAddress().getHostString()); logger.info("链接报告Port:{}", channel.localAddress().getPort()); logger.info("链接报告完毕"); ChannelHandler.channelGroup.add(ctx.channel()); } /** * 当客户端主动断开服务端的链接后,这个通道就是不活跃的。也就是说客户端与服务端的关闭了通信通道并且不可以传输数据 */ @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { logger.info("客户端断开链接{}", ctx.channel().localAddress().toString()); ChannelHandler.channelGroup.remove(ctx.channel()); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { //http if (msg instanceof FullHttprequest) { FullHttpRequest httpRequest = (FullHttpRequest) msg; if (!httpRequest.decoderResult().isSuccess()) { DefaultFullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST); // 返回应答给客户端 if (httpResponse.status().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(httpResponse.status().toString(), CharsetUtil.UTF_8); httpResponse.content().writeBytes(buf); buf.release(); } // 如果是非Keep-Alive,关闭连接 ChannelFuture f = ctx.channel().writeAndFlush(httpResponse); if (httpResponse.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } return; } WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory("ws:/" + ctx.channel() + "/websocket", null, false); handshaker = wsFactory.newHandshaker(httpRequest); if (null == handshaker) { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); } else { handshaker.handshake(ctx.channel(), httpRequest); } return; } //ws if (msg instanceof WebSocketFrame) { WebSocketFrame webSocketFrame = (WebSocketFrame) msg; //关闭请求 if (webSocketFrame instanceof CloseWebSocketFrame) { handshaker.close(ctx.channel(), (CloseWebSocketFrame) webSocketFrame.retain()); return; } //ping请求 if (webSocketFrame instanceof PingWebSocketFrame) { ctx.channel().write(new PongWebSocketFrame(webSocketFrame.content().retain())); return; } //只支持文本格式,不支持二进制消息 if (!(webSocketFrame instanceof TextWebSocketFrame)) { throw new Exception("仅支持文本格式"); } String request = ((TextWebSocketFrame) webSocketFrame).text(); System.out.println("服务端收到:" + request); ClientMsgProtocol ClientMsgProtocol = JSON.parseobject(request, ClientMsgProtocol.class); //1请求个人信息 if (1 == clientMsgProtocol.getType()) { ctx.channel().writeAndFlush(MsgUtil.buildMsgOwner(ctx.channel().id().toString())); return; } //群发消息 if (2 == clientMsgProtocol.getType()) { TextWebSocketFrame textWebSocketFrame = MsgUtil.buildMsgAll(ctx.channel().id().toString(), clientMsgProtocol.getMsgInfo()); ChannelHandler.channelGroup.writeAndFlush(textWebSocketFrame); } } } /** * 抓住异常,当发生异常的时候,可以做一些相应的处理,比如打印日志、关闭链接 */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); logger.info("异常信息:rn" + cause.getMessage()); } }
util/MsgUtil.java *消息构建工具类
/** * 消息传输协议 * 虫洞栈:https://bugstack.cn * Create by fuzhengwei on @2019 */ public class MsgUtil { public static TextWebSocketFrame buildMsgAll(String channelId, String msgInfo) { //模拟头像 int i = Math.abs(channelId.hashCode()) % 10; ServerMsgProtocol msg = new ServerMsgProtocol(); msg.setType(2); //链接信息;1自发信息、2群发消息 msg.setChannelId(channelId); msg.setUserHeadImg("head" + i + ".jpg"); msg.setMsgInfo(msgInfo); return new TextWebSocketFrame(JSON.toJSONString(msg)); } public static TextWebSocketFrame buildMsgOwner(String channelId) { ServerMsgProtocol msg = new ServerMsgProtocol(); msg.setType(1); //链接信息;1链接信息、2消息信息 msg.setChannelId(channelId); return new TextWebSocketFrame(JSON.toJSONString(msg)); } }
resources/application.yml *基础配置信息,包括了;应用端口、netty服务端端口等
server: port: 8080 netty: host: 127.0.0.1 port: 7397 spring: mvc: view: prefix: /WEB-INF/ suffix: .jsp
WEB-INF/index.jsp *模仿微信聊天界面
margin:0 auto; margin-top:19px; margin-left:12px; float:left; border:1px solid #666666;border-radius:3px;-moz-border-radius:3px;">
padding-top:5px; padding-bottom:5px; width:210px; background-color:#DBD9D8;border-radius:3px;-moz-border-radius:3px; float:left; margin-left:20px; font-size:12px; color:#333333;text-indent:27px;"> http://bugstack.cn
哪咤宝 付政委:[图片] bugstack虫洞栈 北京程序猿-小白:netty开发.. 咸鱼江湖 背包冲:情人节没礼物,不存.. 整条街最靓 公司-老板:[文件]下个Q的KPI Sniper Sniper:雨后天晴写下,年华.. 星星点灯照亮我的家门 王老板:不吹牛的说我家77套.. 詹姆斯·高斯林 詹姆斯·高斯林:我所说的都关.. 叮裤猫 叮裤猫:那还第一次见 背锅冲 背锅冲:大树说的,不让去。
测试结果
启动SpringBoot Netty会随着启动
打开网页websocket客户端;http://localhost:8080/index
服务端执行结果
. ____ _ __ _ _ /\ / ___'_ __ _ _(_)_ __ __ _ ( ( )___ | '_ | '_| | '_ / _` | \/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |___, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.4.RELEASE) 2019-08-04 19:44:59.132 INFO 9208 --- [ main] o.i.demo.netty.web.NettyApplication : Starting NettyApplication on JRA1W11T0247 with PID 9208 (E:itstackGITitstack.orgitstack-demo-nettyitstack-demo-netty-2-05targetclasses started by fuzhengwei1 in E:itstackGITitstack.orgitstack-demo-nettyitstack-demo-netty-2-05) 2019-08-04 19:44:59.138 INFO 9208 --- [ main] o.i.demo.netty.web.NettyApplication : No active profile set, falling back to default profiles: default 2019-08-04 19:44:59.437 INFO 9208 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@32cc499f: startup date [Sun Aug 04 19:44:59 CST 2019]; root of context hierarchy 2019-08-04 19:45:00.702 INFO 9208 --- [ main] o.s.b.w.embedded.Tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2019-08-04 19:45:00.738 INFO 9208 --- [ main] o.Apache.catalina.core.StandardService : Starting service [Tomcat] 2019-08-04 19:45:00.738 INFO 9208 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.32 2019-08-04 19:45:00.748 INFO 9208 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:Program FilesJavajdk1.8.0_45bin;C:windowsSunJavabin;C:windowssystem32;C:windows;C:ProgramDataOracleJavajavapath;C:Program Files (x86)Common FilesNetSarang;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:windowssystem32;C:windows;C:windowsSystem32Wbem;C:windowsSystem32WindowsPowerShellv1.0;C:Program FilesIntelIntel(R) Management Engine ComponentsDAL;C:Program FilesIntelIntel(R) Management Engine ComponentsIPT;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsDAL;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsIPT;C:Program FilesJavajdk1.8.0_45/bin;C:Program FilesJavajdk1.8.0_45/jre/bin;D:Program FilesSlikSvnbin;D:Program FilesTortoiseSVNbin;D:Program Files (x86)apache-maven-2.2.1bin;D:Program FilesTortoiseGitbin;D:Program Filesnodejs;D:Program Files (x86)SSH Communications SecuritySSH Secure Shell;C:Usersfuzhengwei1AppDataRoamingnpm;;.] 2019-08-04 19:45:00.985 INFO 9208 --- [ost-startStop-1] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 2019-08-04 19:45:00.994 INFO 9208 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-08-04 19:45:00.997 INFO 9208 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1560 ms 2019-08-04 19:45:01.082 INFO 9208 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 2019-08-04 19:45:01.086 INFO 9208 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2019-08-04 19:45:01.086 INFO 9208 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2019-08-04 19:45:01.086 INFO 9208 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2019-08-04 19:45:01.086 INFO 9208 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2019-08-04 19:45:01.334 INFO 9208 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2019-08-04 19:45:01.497 INFO 9208 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@32cc499f: startup date [Sun Aug 04 19:44:59 CST 2019]; root of context hierarchy 2019-08-04 19:45:01.578 INFO 9208 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/index]}" onto public java.lang.String org.itstack.demo.netty.web.NettyController.index(org.springframework.ui.Model) 2019-08-04 19:45:01.581 INFO 9208 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2019-08-04 19:45:01.582 INFO 9208 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2019-08-04 19:45:01.606 INFO 9208 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2019-08-04 19:45:01.606 INFO 9208 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2019-08-04 19:45:01.673 INFO 9208 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page template: index 2019-08-04 19:45:01.771 INFO 9208 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2019-08-04 19:45:01.832 INFO 9208 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019-08-04 19:45:01.836 INFO 9208 --- [ main] o.i.demo.netty.web.NettyApplication : Started NettyApplication in 3.205 seconds (JVM running for 6.314) 2019-08-04 19:45:02.002 INFO 9208 --- [ main] o.itstack.demo.netty.server.NettyServer : itstack-demo-netty server start done. {关注公众号:bugstack虫洞栈,获取源码} 2019-08-04 19:45:04.850 INFO 9208 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' 2019-08-04 19:45:04.850 INFO 9208 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 2019-08-04 19:45:04.867 INFO 9208 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 17 ms 2019-08-04 19:45:06.137 INFO 9208 --- [ntLoopGroup-3-1] o.i.demo.netty.server.MyServerHandler : 链接报告开始 2019-08-04 19:45:06.137 INFO 9208 --- [ntLoopGroup-3-1] o.i.demo.netty.server.MyServerHandler : 链接报告信息:有一客户端链接到本服务端 2019-08-04 19:45:06.137 INFO 9208 --- [ntLoopGroup-3-1] o.i.demo.netty.server.MyServerHandler : 链接报告IP:127.0.0.1 2019-08-04 19:45:06.137 INFO 9208 --- [ntLoopGroup-3-1] o.i.demo.netty.server.MyServerHandler : 链接报告Port:7397 2019-08-04 19:45:06.137 INFO 9208 --- [ntLoopGroup-3-1] o.i.demo.netty.server.MyServerHandler : 链接报告完毕 服务端收到:{"type":1,"msgInfo":"请求个人信息"} 2019-08-04 19:45:10.590 INFO 9208 --- [ntLoopGroup-3-2] o.i.demo.netty.server.MyServerHandler : 链接报告开始 2019-08-04 19:45:10.590 INFO 9208 --- [ntLoopGroup-3-2] o.i.demo.netty.server.MyServerHandler : 链接报告信息:有一客户端链接到本服务端 2019-08-04 19:45:10.591 INFO 9208 --- [ntLoopGroup-3-2] o.i.demo.netty.server.MyServerHandler : 链接报告IP:127.0.0.1 2019-08-04 19:45:10.591 INFO 9208 --- [ntLoopGroup-3-2] o.i.demo.netty.server.MyServerHandler : 链接报告Port:7397 2019-08-04 19:45:10.591 INFO 9208 --- [ntLoopGroup-3-2] o.i.demo.netty.server.MyServerHandler : 链接报告完毕 服务端收到:{"type":1,"msgInfo":"请求个人信息"} 2019-08-04 19:45:12.374 INFO 9208 --- [ntLoopGroup-3-3] o.i.demo.netty.server.MyServerHandler : 链接报告开始 2019-08-04 19:45:12.374 INFO 9208 --- [ntLoopGroup-3-3] o.i.demo.netty.server.MyServerHandler : 链接报告信息:有一客户端链接到本服务端 2019-08-04 19:45:12.374 INFO 9208 --- [ntLoopGroup-3-3] o.i.demo.netty.server.MyServerHandler : 链接报告IP:127.0.0.1 2019-08-04 19:45:12.374 INFO 9208 --- [ntLoopGroup-3-3] o.i.demo.netty.server.MyServerHandler : 链接报告Port:7397 2019-08-04 19:45:12.374 INFO 9208 --- [ntLoopGroup-3-3] o.i.demo.netty.server.MyServerHandler : 链接报告完毕 服务端收到:{"type":1,"msgInfo":"请求个人信息"} 服务端收到:{"type":2,"msgInfo":"你好在吗,我是bugstack虫洞栈的作者,付政委。"} 服务端收到:{"type":2,"msgInfo":"我在的,我已经关注了这个公众号;bugstack虫洞栈,里面的很多知识都是干货,真的能帮助到我的学习,他还有博客网站;https://bugstack.cn 感谢作者!让我学习到这么多知识。"} 服务端收到:{"type":2,"msgInfo":"呀和!原来这么多人在群里。哈哈哈,大家一起学习真好。我的头像是随机的哦,你们的也是。像公告的信息一样;不平凡的岁月终究来自你每日不停歇的刻苦拼搏,犹如;承遇朝霞,年少正恰,整装戎马,刻印风华。"} Process finished with exit code -1
微信搜索「bugstack虫洞栈」公众号,关注后回复「Netty专题案例」获取本文源码&更多原创专题案例!