Netty 是一個利用 Java 的高級網(wǎng)絡(luò)的能力,隱藏其背后的復(fù)雜性而提供一個易于使用的 API 的客戶端/服務(wù)器框架。
Maven依賴
SpringBootApplication
啟動器中需要new一個NettyServer,并顯式調(diào)用啟動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,這里進(jìn)行配置
/** *NettyServerNetty服務(wù)器配置 *@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)//綁定監(jiān)聽端口 .childHandler(newChannelInitializer
MyChannelHandlerPool
通道組池,管理所有websocket連接
/** *MyChannelHandlerPool *通道組池,管理所有websocket連接 *@authorzhengkai.blog.csdn.net *@date2019-06-12 */ publicclassMyChannelHandlerPool{ publicMyChannelHandlerPool(){} publicstaticChannelGroupchannelGroup=newDefaultChannelGroup(GlobalEventExecutor.INSTANCE); }
MyWebSocketHandler
處理ws一下幾種情況:
channelActive與客戶端建立連接
channelInactive與客戶端斷開連接
channelRead0客戶端發(fā)送消息處理
/** *NettyServerNetty服務(wù)器配置 *@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)//綁定監(jiān)聽端口 .childHandler(newChannelInitializer
socket.html
主要是連接ws,發(fā)送消息,以及消息反饋
Controller
寫好了html當(dāng)然還需要一個controller來引導(dǎo)頁面。
@RestController publicclassIndexController{ @GetMapping("/index") publicModelAndViewindex(){ ModelAndViewmav=newModelAndView("socket"); mav.addObject("uid",RandomUtil.randomNumbers(6)); returnmav; } }
效果演示
改造netty支持url參數(shù)
1.首先,調(diào)整一下加載handler的順序,優(yōu)先MyWebSocketHandler在WebSocketServerProtocolHandler之上。
ch.pipeline().addLast(newMyWebSocketHandler()); ch.pipeline().addLast(newWebSocketServerProtocolHandler("/ws",null,true,65536*10)); 2.其次,改造MyWebSocketHandler的channelRead方法,首次連接會是一個FullHttpRequest類型,可以通過FullHttpRequest.uri()獲取完整ws的URL地址,之后接受信息的話,會是一個TextWebSocketFrame類型。
publicclassMyWebSocketHandlerextendsSimpleChannelInboundHandler
socket=newWebSocket("ws://127.0.0.1:12345/ws?uid=666&gid=777"); 4.改造后控制臺輸出情況
收到新連接 與客戶端建立連接,通道開啟! 接收到的參數(shù)是:{"uid":"666","gid":"777"} /ws 客戶端收到服務(wù)器數(shù)據(jù):142531:這里輸入消息 客戶端收到服務(wù)器數(shù)據(jù):142531:這里輸入消息 客戶端收到服務(wù)器數(shù)據(jù):142531:這里輸入消息
failed: WebSocket opening handshake timed out
聽說是ssl wss的情況下才會出現(xiàn),來自 @around-gao 的解決方法: 把MyWebSocketHandler和WebSocketServerProtocolHandler調(diào)下順序就好了。
-
服務(wù)器
+關(guān)注
關(guān)注
13文章
9723瀏覽量
87407 -
API
+關(guān)注
關(guān)注
2文章
1563瀏覽量
63622 -
網(wǎng)絡(luò)
+關(guān)注
關(guān)注
14文章
7782瀏覽量
90496
原文標(biāo)題:Spring Boot + Netty + WebSocket 實現(xiàn)消息推送
文章出處:【微信號:AndroidPush,微信公眾號:Android編程精選】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
基于多路復(fù)用模型的Netty框架
基于阿里云移動推送的移動應(yīng)用推送模式最佳實踐
如何實現(xiàn)服務(wù)器自動推送消息?
怎么去理解netty
怎樣使用springboot整合netty來開發(fā)一套高性能的通信系統(tǒng)呢
如何采用mqtt協(xié)議實現(xiàn)物聯(lián)網(wǎng)模塊消息推送?
單片機MQTT如何實現(xiàn)推送的簡單使用

Springboot整合netty框架實現(xiàn)終端、通訊板子(單片機)TCP/UDP通信案例

詳解Netty高性能異步事件驅(qū)動的網(wǎng)絡(luò)框架
Netty如何做到單機百萬并發(fā)?
netty推送消息接口及實現(xiàn)
jdk17下netty導(dǎo)致堆內(nèi)存瘋漲原因排查

評論