基于Netty搭建WebSocket,模仿微信聊天頁(yè)面(websocket+netty)
作者:小傅哥
博客:https://bugstack.cn
沉淀、分享、成長(zhǎng),讓自己和他人都能有所收獲!
前言介紹
本章節(jié)我們模仿微信聊天頁(yè)面,開(kāi)發(fā)一個(gè)基于Netty搭建WebSocket通信案例。Netty的應(yīng)用方面非常廣;聊天、MQ、RPC、數(shù)據(jù)等等,在5G到來(lái)的時(shí)候更加需要大量數(shù)據(jù)傳輸,Netty的應(yīng)用也會(huì)更加廣闊。
- 這個(gè)案例使用SpringBoot Netty WebSocket搭建功能。
- 使用Netty提供的HttpServerCodec、HttpObjectAggregator、ChunkedWriteHandler進(jìn)行編碼解碼處理。
- 通信邏輯盡可能簡(jiǎn)化到只了解根本,便于后續(xù)個(gè)人應(yīng)用及開(kāi)發(fā)的拓展。 客戶(hù)端鏈接成功后,向服務(wù)端發(fā)送請(qǐng)求獲取個(gè)人信息,也可以拓展為登錄請(qǐng)求。獲取個(gè)人信息后,就可以知道差異化展示消息到頁(yè)面。
開(kāi)發(fā)環(huán)境
- jdk1.8【jdk1.7以下只能部分支持netty】
- Netty4.1.36.Final【netty3.x 4.x 5每次的變化較大,接口類(lèi)名也隨著變化】
- jquery.min.js、jquery.serialize-object.min.js
代碼示例
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
演示部分重點(diǎn)代碼塊,完整代碼下載關(guān)注公眾號(hào);bugstack蟲(chóng)洞棧
server/MyChannelInitializer.java *websocket處理協(xié)議
/** * 消息傳輸協(xié)議 * 蟲(chóng)洞棧:https://bugstack.cn * Create by fuzhengwei on @2019 */public class MyChannelInitializer extends ChannelInitializer<SocketChannel> { @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()); // 在管道中添加我們自己的接收數(shù)據(jù)實(shí)現(xiàn)方法 channel.pipeline().addLast(new MyServerHandler()); }}
server/MyServerHandler.java *處理websocket消息信息
/** * 消息傳輸協(xié)議 * 蟲(chóng)洞棧:https://bugstack.cn * Create by fuzhengwei on @2019 */public class MyServerHandler extends ChannelInboundHandlerAdapter { private Logger logger = LoggerFactory.getLogger(MyServerHandler.class); private WebSocketServerHandshaker handshaker; /** * 當(dāng)客戶(hù)端主動(dòng)鏈接服務(wù)端的鏈接后,這個(gè)通道就是活躍的了。也就是客戶(hù)端與服務(wù)端建立了通信通道并且可以傳輸數(shù)據(jù) */ @Override public void channelActive(ChannelHandlerContext ctx) throws exception { SocketChannel channel = (SocketChannel) ctx.channel(); logger.info("鏈接報(bào)告開(kāi)始"); logger.info("鏈接報(bào)告信息:有一客戶(hù)端鏈接到本服務(wù)端"); logger.info("鏈接報(bào)告IP:{}", channel.localAddress().getHostString()); logger.info("鏈接報(bào)告Port:{}", channel.localAddress().getPort()); logger.info("鏈接報(bào)告完畢"); ChannelHandler.channelGroup.add(ctx.channel()); } /** * 當(dāng)客戶(hù)端主動(dòng)斷開(kāi)服務(wù)端的鏈接后,這個(gè)通道就是不活躍的。也就是說(shuō)客戶(hù)端與服務(wù)端的關(guān)閉了通信通道并且不可以傳輸數(shù)據(jù) */ @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { logger.info("客戶(hù)端斷開(kāi)鏈接{}", 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); // 返回應(yīng)答給客戶(hù)端 if (httpResponse.status().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(httpResponse.status().toString(), CharsetUtil.UTF_8); httpResponse.content().writeBytes(buf); buf.release(); } // 如果是非Keep-Alive,關(guān)閉連接 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; //關(guān)閉請(qǐng)求 if (webSocketFrame instanceof CloseWebSocketFrame) { handshaker.close(ctx.channel(), (CloseWebSocketFrame) webSocketFrame.retain()); return; } //ping請(qǐng)求 if (webSocketFrame instanceof PingWebSocketFrame) { ctx.channel().write(new PongWebSocketFrame(webSocketFrame.content().retain())); return; } //只支持文本格式,不支持二進(jìn)制消息 if (!(webSocketFrame instanceof TextWebSocketFrame)) { throw new Exception("僅支持文本格式"); } String request = ((TextWebSocketFrame) webSocketFrame).text(); System.out.println("服務(wù)端收到:" request); ClientMsgProtocol ClientMsgProtocol = JSON.parseObject(request, ClientMsgProtocol.class); //1請(qǐng)求個(gè)人信息 if (1 == clientMsgProtocol.getType()) { ctx.channel().writeAndFlush(MsgUtil.buildMsgOwner(ctx.channel().id().toString())); return; } //群發(fā)消息 if (2 == clientMsgProtocol.getType()) { TextWebSocketFrame textWebSocketFrame = MsgUtil.buildMsgAll(ctx.channel().id().toString(), clientMsgProtocol.getMsgInfo()); ChannelHandler.channelGroup.writeAndFlush(textWebSocketFrame); } } } /** * 抓住異常,當(dāng)發(fā)生異常的時(shí)候,可以做一些相應(yīng)的處理,比如打印日志、關(guān)閉鏈接 */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); logger.info("異常信息:rn" cause.getMessage()); }}
util/MsgUtil.java *消息構(gòu)建工具類(lèi)
/** * 消息傳輸協(xié)議 * 蟲(chóng)洞棧: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自發(fā)信息、2群發(fā)消息 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 *基礎(chǔ)配置信息,包括了;應(yīng)用端口、netty服務(wù)端端口等
server: port: 8080netty: host: 127.0.0.1 port: 7397spring: mvc: view: prefix: /WEB-INF/ suffix: .jsp
WEB-INF/index.jsp *模仿微信聊天界面
<!-- left --> <div style="width:60px; height:667px; background-color:#2D2B2A; float:left;"> <!-- 頭像 --> <div style="width:35px; height:35px; margin:0 auto; margin-top:19px; margin-left:12px; float:left; border:1px solid #666666;border-radius:3px;-moz-border-radius:3px;"> <img src="res/img/bugstack.png" width="35px" height="35px"/> </div> <!-- 聊天 --> <div style="width:28px; height:28px; margin:0 auto; margin-top:25px; margin-left:16px; float:left;"> <img src="res/img/chat.png" width="28px" height="28px"/> </div> <!-- 好友 --> <div style="width:28px; height:28px; margin:0 auto; margin-top:20px; margin-left:16px; float:left;"> <img src="res/img/friend.png" width="28px" height="28px"/> </div> <!-- 收藏 --> <div style="width:28px; height:28px; margin:0 auto; margin-top:20px; margin-left:16px; float:left;"> <img src="res/img/collection.png" width="28px" height="28px"/> </div> <!-- 設(shè)置 --> <div style="width:20px; height:20px; margin:0 auto; margin-left:20px; float:left; position:absolute;bottom:0; margin-bottom:12px;"> <img src="res/img/set.png" width="20px" height="20px"/> </div> </div> <!-- center --> <div style="width:250px; height:667px; background-color:#EBE9E8; float:left;"> <div style=" background-image:url(res/img/search.png); background-repeat:no-repeat;margin:0 auto; margin-top:25px; 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 </div> <!-- friendList --> <div id="friendList" style="float:left; margin-top:5px;"> <div style="width:250px; height:65px;"> <table style="width:240px; height:60px; margin:0 auto; margin-top:2px; background-color:#E5E5E5;"> <tr> <td rowspan="2" width="50"><img src="res/img/head1.jpg" width="50px" height="50px" style="border-radius:3px;-moz-border-radius:3px;"/></td> <td style="color:#333333; text-indent:5px; font-size:12px; vertical-align:bottom; font-weight:bolder;">哪咤寶</td> </tr> <tr> <td style="color:#999999; text-indent:5px; font-size:10px;">付政委:[圖片]</td> </tr> </table> <table style="width:240px; height:60px; margin:0 auto; margin-top:2px; background-color:#C9C7C6;"> <tr> <td rowspan="2" width="50"><img src="res/img/head2.jpg" width="50px" height="50px" style="border-radius:3px;-moz-border-radius:3px;"/></td> <td style="color:#333333; text-indent:5px; font-size:12px; vertical-align:bottom; font-weight:bolder;">bugstack蟲(chóng)洞棧</td> </tr> <tr> <td style="color:#999999; text-indent:5px; font-size:10px;">北京程序猿-小白:netty開(kāi)發(fā)..</td> </tr> </table> <table style="width:240px; height:60px; margin:0 auto; margin-top:2px; background-color:#E5E5E5;"> <tr> <td rowspan="2" width="50"><img src="res/img/head3.jpg" width="50px" height="50px" style="border-radius:3px;-moz-border-radius:3px;"/></td> <td style="color:#333333; text-indent:5px; font-size:12px; vertical-align:bottom; font-weight:bolder;">咸魚(yú)江湖</td> </tr> <tr> <td style="color:#999999; text-indent:5px; font-size:10px;">背包沖:情人節(jié)沒(méi)禮物,不存..</td> </tr> </table> <table style="width:240px; height:60px; margin:0 auto; margin-top:2px; background-color:#E5E5E5;"> <tr> <td rowspan="2" width="50"><img src="res/img/head4.jpg" width="50px" height="50px" style="border-radius:3px;-moz-border-radius:3px;"/></td> <td style="color:#333333; text-indent:5px; font-size:12px; vertical-align:bottom; font-weight:bolder;">整條街最靚</td> </tr> <tr> <td style="color:#999999; text-indent:5px; font-size:10px;">公司-老板:[文件]下個(gè)Q的KPI</td> </tr> </table> <table style="width:240px; height:60px; margin:0 auto; margin-top:2px; background-color:#E5E5E5;"> <tr> <td rowspan="2" width="50"><img src="res/img/head5.jpg" width="50px" height="50px" style="border-radius:3px;-moz-border-radius:3px;"/></td> <td style="color:#333333; text-indent:5px; font-size:12px; vertical-align:bottom; font-weight:bolder;">Sniper</td> </tr> <tr> <td style="color:#999999; text-indent:5px; font-size:10px;">Sniper:雨后天晴寫(xiě)下,年華..</td> </tr> </table> <table style="width:240px; height:60px; margin:0 auto; margin-top:2px; background-color:#E5E5E5;"> <tr> <td rowspan="2" width="50"><img src="res/img/head7.jpg" width="50px" height="50px" style="border-radius:3px;-moz-border-radius:3px;"/></td> <td style="color:#333333; text-indent:5px; font-size:12px; vertical-align:bottom; font-weight:bolder;">星星點(diǎn)燈照亮我的家門(mén)</td> </tr> <tr> <td style="color:#999999; text-indent:5px; font-size:10px;">王老板:不吹牛的說(shuō)我家77套..</td> </tr> </table> <table style="width:240px; height:60px; margin:0 auto; margin-top:2px; background-color:#E5E5E5;"> <tr> <td rowspan="2" width="50"><img src="res/img/head8.jpg" width="50px" height="50px" style="border-radius:3px;-moz-border-radius:3px;"/></td> <td style="color:#333333; text-indent:5px; font-size:12px; vertical-align:bottom; font-weight:bolder;">詹姆斯·高斯林</td> </tr> <tr> <td style="color:#999999; text-indent:5px; font-size:10px;">詹姆斯·高斯林:我所說(shuō)的都關(guān)..</td> </tr> </table> <table style="width:240px; height:60px; margin:0 auto; margin-top:2px; background-color:#E5E5E5;"> <tr> <td rowspan="2" width="50"><img src="res/img/head9.jpg" width="50px" height="50px" style="border-radius:3px;-moz-border-radius:3px;"/></td> <td style="color:#333333; text-indent:5px; font-size:12px; vertical-align:bottom; font-weight:bolder;">叮褲貓</td> </tr> <tr> <td style="color:#999999; text-indent:5px; font-size:10px;">叮褲貓:那還第一次見(jiàn)</td> </tr> </table> <table style="width:240px; height:60px; margin:0 auto; margin-top:2px; background-color:#E5E5E5;"> <tr> <td rowspan="2" width="50"><img src="res/img/head10.jpg" width="50px" height="50px" style="border-radius:3px;-moz-border-radius:3px;"/></td> <td style="color:#333333; text-indent:5px; font-size:12px; vertical-align:bottom; font-weight:bolder;">背鍋沖</td> </tr> <tr> <td style="color:#999999; text-indent:5px; font-size:10px;">背鍋沖:大樹(shù)說(shuō)的,不讓去。</td> </tr> </table> </div> </div> </div> ... ...
測(cè)試結(jié)果
啟動(dòng)SpringBoot Netty會(huì)隨著啟動(dòng)
打開(kāi)網(wǎng)頁(yè)websocket客戶(hù)端;http://localhost:8080/index
服務(wù)端執(zhí)行結(jié)果
. ____ _ __ _ _ / / ___'_ __ _ _(_)_ __ __ _ ( ( )___ | '_ | '_| | '_ / _` | / ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |___, | / / / / =========|_|==============|___/=/_/_/_/ :: 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: default2019-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 hierarchy2019-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.322019-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 WebApplicationContext2019-08-04 19:45:00.997 INFO 9208 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1560 ms2019-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 hierarchy2019-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<java.util.Map<java.lang.String, java.lang.Object>> 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: index2019-08-04 19:45:01.771 INFO 9208 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2019-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. {關(guān)注公眾號(hào):bugstack蟲(chóng)洞棧,獲取源碼}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 started2019-08-04 19:45:04.867 INFO 9208 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 17 ms2019-08-04 19:45:06.137 INFO 9208 --- [ntLoopGroup-3-1] o.i.demo.netty.server.MyServerHandler : 鏈接報(bào)告開(kāi)始2019-08-04 19:45:06.137 INFO 9208 --- [ntLoopGroup-3-1] o.i.demo.netty.server.MyServerHandler : 鏈接報(bào)告信息:有一客戶(hù)端鏈接到本服務(wù)端2019-08-04 19:45:06.137 INFO 9208 --- [ntLoopGroup-3-1] o.i.demo.netty.server.MyServerHandler : 鏈接報(bào)告IP:127.0.0.12019-08-04 19:45:06.137 INFO 9208 --- [ntLoopGroup-3-1] o.i.demo.netty.server.MyServerHandler : 鏈接報(bào)告Port:73972019-08-04 19:45:06.137 INFO 9208 --- [ntLoopGroup-3-1] o.i.demo.netty.server.MyServerHandler : 鏈接報(bào)告完畢服務(wù)端收到:{"type":1,"msgInfo":"請(qǐng)求個(gè)人信息"}2019-08-04 19:45:10.590 INFO 9208 --- [ntLoopGroup-3-2] o.i.demo.netty.server.MyServerHandler : 鏈接報(bào)告開(kāi)始2019-08-04 19:45:10.590 INFO 9208 --- [ntLoopGroup-3-2] o.i.demo.netty.server.MyServerHandler : 鏈接報(bào)告信息:有一客戶(hù)端鏈接到本服務(wù)端2019-08-04 19:45:10.591 INFO 9208 --- [ntLoopGroup-3-2] o.i.demo.netty.server.MyServerHandler : 鏈接報(bào)告IP:127.0.0.12019-08-04 19:45:10.591 INFO 9208 --- [ntLoopGroup-3-2] o.i.demo.netty.server.MyServerHandler : 鏈接報(bào)告Port:73972019-08-04 19:45:10.591 INFO 9208 --- [ntLoopGroup-3-2] o.i.demo.netty.server.MyServerHandler : 鏈接報(bào)告完畢服務(wù)端收到:{"type":1,"msgInfo":"請(qǐng)求個(gè)人信息"}2019-08-04 19:45:12.374 INFO 9208 --- [ntLoopGroup-3-3] o.i.demo.netty.server.MyServerHandler : 鏈接報(bào)告開(kāi)始2019-08-04 19:45:12.374 INFO 9208 --- [ntLoopGroup-3-3] o.i.demo.netty.server.MyServerHandler : 鏈接報(bào)告信息:有一客戶(hù)端鏈接到本服務(wù)端2019-08-04 19:45:12.374 INFO 9208 --- [ntLoopGroup-3-3] o.i.demo.netty.server.MyServerHandler : 鏈接報(bào)告IP:127.0.0.12019-08-04 19:45:12.374 INFO 9208 --- [ntLoopGroup-3-3] o.i.demo.netty.server.MyServerHandler : 鏈接報(bào)告Port:73972019-08-04 19:45:12.374 INFO 9208 --- [ntLoopGroup-3-3] o.i.demo.netty.server.MyServerHandler : 鏈接報(bào)告完畢服務(wù)端收到:{"type":1,"msgInfo":"請(qǐng)求個(gè)人信息"}服務(wù)端收到:{"type":2,"msgInfo":"你好在嗎,我是bugstack蟲(chóng)洞棧的作者,付政委。"}服務(wù)端收到:{"type":2,"msgInfo":"我在的,我已經(jīng)關(guān)注了這個(gè)公眾號(hào);bugstack蟲(chóng)洞棧,里面的很多知識(shí)都是干貨,真的能幫助到我的學(xué)習(xí),他還有博客網(wǎng)站;https://bugstack.cn 感謝作者!讓我學(xué)習(xí)到這么多知識(shí)。"}服務(wù)端收到:{"type":2,"msgInfo":"呀和!原來(lái)這么多人在群里。哈哈哈,大家一起學(xué)習(xí)真好。我的頭像是隨機(jī)的哦,你們的也是。像公告的信息一樣;不平凡的歲月終究來(lái)自你每日不停歇的刻苦拼搏,猶如;承遇朝霞,年少正恰,整裝戎馬,刻印風(fēng)華。"}Process finished with exit code -1
微信搜索「bugstack蟲(chóng)洞棧」公眾號(hào),關(guān)注后回復(fù)「Netty專(zhuān)題案例」獲取本文源碼&更多原創(chuàng)專(zhuān)題案例!