电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Springboot+Netty+Websocket實(shí)現(xiàn)消息推送實(shí)例

瀏覽:3日期:2023-03-26 08:29:03
前言

WebSocket 使得客戶(hù)端和服務(wù)器之間的數(shù)據(jù)交換變得更加簡(jiǎn)單,允許服務(wù)端主動(dòng)向客戶(hù)端推送數(shù)據(jù)。在 WebSocket API 中,瀏覽器和服務(wù)器只需要完成一次握手,兩者之間就直接可以創(chuàng)建持久性的連接,并進(jìn)行雙向數(shù)據(jù)傳輸。

Netty框架的優(yōu)勢(shì)

1. API使用簡(jiǎn)單,開(kāi)發(fā)門(mén)檻低; 2. 功能強(qiáng)大,預(yù)置了多種編解碼功能,支持多種主流協(xié)議; 3. 定制能力強(qiáng),可以通過(guò)ChannelHandler對(duì)通信框架進(jìn)行靈活地?cái)U(kuò)展; 4. 性能高,通過(guò)與其他業(yè)界主流的NIO框架對(duì)比,Netty的綜合性能最優(yōu); 5. 成熟、穩(wěn)定,Netty修復(fù)了已經(jīng)發(fā)現(xiàn)的所有JDK NIO BUG,業(yè)務(wù)開(kāi)發(fā)人員不需要再為NIO的BUG而煩惱

提示:以下是本篇文章正文內(nèi)容,下面案例可供參考

一、引入netty依賴(lài)

<dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.48.Final</version></dependency>二、使用步驟

1.引入基礎(chǔ)配置類(lèi)

package com.test.netty;public enum Cmd { START('000', '連接成功'), WMESSAGE('001', '消息提醒'), ; private String cmd; private String desc; Cmd(String cmd, String desc) { this.cmd = cmd; this.desc = desc; } public String getCmd() { return cmd; } public String getDesc() { return desc; }}

2.netty服務(wù)啟動(dòng)監(jiān)聽(tīng)器

package com.test.netty;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelOption;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioServerSocketChannel;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.ApplicationRunner;import org.springframework.context.annotation.Bean;import org.springframework.stereotype.Component;/** * @author test * <p> * 服務(wù)啟動(dòng)監(jiān)聽(tīng)器 **/@Slf4j@Componentpublic class NettyServer { @Value('${server.netty.port}') private int port; @Autowired private ServerChannelInitializer serverChannelInitializer; @Bean ApplicationRunner nettyRunner() { return args -> { //new 一個(gè)主線(xiàn)程組 EventLoopGroup bossGroup = new NioEventLoopGroup(1); //new 一個(gè)工作線(xiàn)程組 EventLoopGroup workGroup = new NioEventLoopGroup(); ServerBootstrap bootstrap = new ServerBootstrap() .group(bossGroup, workGroup) .channel(NioServerSocketChannel.class) .childHandler(serverChannelInitializer) //設(shè)置隊(duì)列大小 .option(ChannelOption.SO_BACKLOG, 1024) // 兩小時(shí)內(nèi)沒(méi)有數(shù)據(jù)的通信時(shí),TCP會(huì)自動(dòng)發(fā)送一個(gè)活動(dòng)探測(cè)數(shù)據(jù)報(bào)文 .childOption(ChannelOption.SO_KEEPALIVE, true); //綁定端口,開(kāi)始接收進(jìn)來(lái)的連接 try { ChannelFuture future = bootstrap.bind(port).sync(); log.info('服務(wù)器啟動(dòng)開(kāi)始監(jiān)聽(tīng)端口: {}', port); future.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { //關(guān)閉主線(xiàn)程組 bossGroup.shutdownGracefully(); //關(guān)閉工作線(xiàn)程組 workGroup.shutdownGracefully(); } }; }}

3.netty服務(wù)端處理器

package com.test.netty;import com.test.common.util.JsonUtil;import io.netty.channel.Channel;import io.netty.channel.ChannelHandler;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;import lombok.Data;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import java.net.URLDecoder;import java.util.*;/** * @author test * <p> * netty服務(wù)端處理器 **/@Slf4j@Component@ChannelHandler.Sharablepublic class NettyServerHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> { @Autowired private ServerChannelCache cache; private static final String dataKey = 'test='; @Data public static class ChannelCache { } /** * 客戶(hù)端連接會(huì)觸發(fā) */ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { Channel channel = ctx.channel(); log.info('通道連接已打開(kāi),ID->{}......', channel.id().asLongText()); } @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof WebSocketServerProtocolHandler.HandshakeComplete) { Channel channel = ctx.channel(); WebSocketServerProtocolHandler.HandshakeComplete handshakeComplete = (WebSocketServerProtocolHandler.HandshakeComplete) evt; String requestUri = handshakeComplete.requestUri(); requestUri = URLDecoder.decode(requestUri, 'UTF-8'); log.info('HANDSHAKE_COMPLETE,ID->{},URI->{}', channel.id().asLongText(), requestUri); String socketKey = requestUri.substring(requestUri.lastIndexOf(dataKey) + dataKey.length()); if (socketKey.length() > 0) { cache.add(socketKey, channel); this.send(channel, Cmd.DOWN_START, null); } else { channel.disconnect(); ctx.close(); } } super.userEventTriggered(ctx, evt); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { Channel channel = ctx.channel(); log.info('通道連接已斷開(kāi),ID->{},用戶(hù)ID->{}......', channel.id().asLongText(), cache.getCacheId(channel)); cache.remove(channel); } /** * 發(fā)生異常觸發(fā) */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { Channel channel = ctx.channel(); log.error('連接出現(xiàn)異常,ID->{},用戶(hù)ID->{},異常->{}......', channel.id().asLongText(), cache.getCacheId(channel), cause.getMessage(), cause); cache.remove(channel); ctx.close(); } /** * 客戶(hù)端發(fā)消息會(huì)觸發(fā) */ @Override protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception { try { // log.info('接收到客戶(hù)端發(fā)送的消息:{}', msg.text()); ctx.channel().writeAndFlush(new TextWebSocketFrame(JsonUtil.toString(Collections.singletonMap('cmd', '100')))); } catch (Exception e) { log.error('消息處理異常:{}', e.getMessage(), e); } } public void send(Cmd cmd, String id, Object obj) { HashMap<String, Channel> channels = cache.get(id); if (channels == null) { return; } Map<String, Object> data = new LinkedHashMap<>(); data.put('cmd', cmd.getCmd()); data.put('data', obj); String msg = JsonUtil.toString(data); log.info('服務(wù)器下發(fā)消息: {}', msg); channels.values().forEach(channel -> { channel.writeAndFlush(new TextWebSocketFrame(msg)); }); } public void send(Channel channel, Cmd cmd, Object obj) { Map<String, Object> data = new LinkedHashMap<>(); data.put('cmd', cmd.getCmd()); data.put('data', obj); String msg = JsonUtil.toString(data); log.info('服務(wù)器下發(fā)消息: {}', msg); channel.writeAndFlush(new TextWebSocketFrame(msg)); }}

4.netty服務(wù)端緩存類(lèi)

package com.test.netty;import io.netty.channel.Channel;import io.netty.util.AttributeKey;import org.springframework.stereotype.Component;import java.util.HashMap;import java.util.concurrent.ConcurrentHashMap;@Componentpublic class ServerChannelCache { private static final ConcurrentHashMap<String, HashMap<String, Channel>> CACHE_MAP = new ConcurrentHashMap<>(); private static final AttributeKey<String> CHANNEL_ATTR_KEY = AttributeKey.valueOf('test'); public String getCacheId(Channel channel) { return channel.attr(CHANNEL_ATTR_KEY).get(); } public void add(String cacheId, Channel channel) { channel.attr(CHANNEL_ATTR_KEY).set(cacheId); HashMap<String, Channel> hashMap = CACHE_MAP.get(cacheId); if (hashMap == null) { hashMap = new HashMap<>(); } hashMap.put(channel.id().asShortText(), channel); CACHE_MAP.put(cacheId, hashMap); } public HashMap<String, Channel> get(String cacheId) { if (cacheId == null) { return null; } return CACHE_MAP.get(cacheId); } public void remove(Channel channel) { String cacheId = getCacheId(channel); if (cacheId == null) { return; } HashMap<String, Channel> hashMap = CACHE_MAP.get(cacheId); if (hashMap == null) { hashMap = new HashMap<>(); } hashMap.remove(channel.id().asShortText()); CACHE_MAP.put(cacheId, hashMap); }}

5.netty服務(wù)初始化器

package com.test.netty;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelPipeline;import io.netty.channel.socket.SocketChannel;import io.netty.handler.codec.http.HttpObjectAggregator;import io.netty.handler.codec.http.HttpServerCodec;import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;import io.netty.handler.stream.ChunkedWriteHandler;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;/** * @author test * <p> * netty服務(wù)初始化器 **/@Componentpublic class ServerChannelInitializer extends ChannelInitializer<SocketChannel> { @Autowired private NettyServerHandler nettyServerHandler; @Override protected void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new ChunkedWriteHandler()); pipeline.addLast(new HttpObjectAggregator(8192)); pipeline.addLast(new WebSocketServerProtocolHandler('/test.io', true, 5000)); pipeline.addLast(nettyServerHandler); }}

6.html測(cè)試

<!DOCTYPE HTML><html> <head> <meta charset='utf-8'> <title>test</title> <script type='text/javascript'> function WebSocketTest() { if ('WebSocket' in window) { alert('您的瀏覽器支持 WebSocket!');// 打開(kāi)一個(gè) web socket var ws = new WebSocket('ws://localhost:port/test.io');ws.onopen = function() { // Web Socket 已連接上,使用 send() 方法發(fā)送數(shù)據(jù) ws.send('發(fā)送數(shù)據(jù)'); alert('數(shù)據(jù)發(fā)送中...'); };ws.onmessage = function (evt) { var received_msg = evt.data; alert('數(shù)據(jù)已接收...'); };ws.onclose = function() { // 關(guān)閉 websocket alert('連接已關(guān)閉...'); }; } else { // 瀏覽器不支持 WebSocket alert('您的瀏覽器不支持 WebSocket!'); } } </script> </head> <body> <div id='sse'> <a href='javascript:WebSocketTest()' rel='external nofollow' >運(yùn)行 WebSocket</a> </div> </body></html>

7.vue測(cè)試

mounted() { this.initWebsocket(); }, methods: { initWebsocket() { let websocket = new WebSocket(’ws://localhost:port/test.io?test=123456’); websocket.onmessage = (event) => { let msg = JSON.parse(event.data); switch (msg.cmd) { case '000': this.$message({type: ’success’,message: '建立實(shí)時(shí)連接成功!',duration: 1000 }) setInterval(()=>{websocket.send('heartbeat')},60*1000); break; case '001': this.$message.warning('收到一條新的信息,請(qǐng)及時(shí)查看!') break; } } websocket.onclose = () => { setTimeout(()=>{ this.initWebsocket(); },30*1000); } websocket.onerror = () => { setTimeout(()=>{ this.initWebsocket(); },30*1000); } }, },![在這里插入圖片描述](https://img-blog.csdnimg.cn/20210107160420568.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d1X3Fpbmdfc29uZw==,size_16,color_FFFFFF,t_70#pic_center)

8.服務(wù)器下發(fā)消息

@Autowiredprivate NettyServerHandler nettyServerHandler;nettyServerHandler.send(CmdWeb.WMESSAGE, id, message);

到此這篇關(guān)于Springboot+Netty+Websocket實(shí)現(xiàn)消息推送實(shí)例的文章就介紹到這了,更多相關(guān)Springboot Websocket消息推送內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 球盟会·(中国)官方网站 | 水性绝缘漆_凡立水_绝缘漆树脂_环保绝缘漆-深圳维特利环保材料有限公司 | 玉米深加工机械,玉米加工设备,玉米加工机械等玉米深加工设备制造商-河南成立粮油机械有限公司 | 纯水电导率测定仪-万用气体检测仪-低钠测定仪-米沃奇科技(北京)有限公司www.milwaukeeinst.cn 锂辉石检测仪器,水泥成分快速分析仪-湘潭宇科分析仪器有限公司 手术室净化装修-手术室净化工程公司-华锐手术室净化厂家 | MOOG伺服阀维修,ATOS比例流量阀维修,伺服阀维修-上海纽顿液压设备有限公司 | 广州展览设计公司_展台设计搭建_展位设计装修公司-众派展览装饰 广州展览制作工厂—[优简]直营展台制作工厂_展会搭建资质齐全 | 专业的新乡振动筛厂家-振动筛品质保障-环保振动筛价格—新乡市德科筛分机械有限公司 | 南京交通事故律师-专打交通事故的南京律师 | 全自动五线打端沾锡机,全自动裁线剥皮双头沾锡机,全自动尼龙扎带机-东莞市海文能机械设备有限公司 | 中国品牌排名投票_十大品牌榜单_中国著名品牌【中国品牌榜】 | 沉降天平_沉降粒度仪_液体比重仪-上海方瑞仪器有限公司 | 翅片管散热器价格_钢制暖气片报价_钢制板式散热器厂家「河北冀春暖气片有限公司」 | 进口便携式天平,外校_十万分之一分析天平,奥豪斯工业台秤,V2000防水秤-重庆珂偌德科技有限公司(www.crdkj.com) | 广东西屋电气有限公司-广东西屋电气有限公司 | 电缆桥架生产厂家_槽式/梯式_热镀锌线槽_广东东莞雷正电气 | POS机办理_个人POS机免费领取 - 银联POS机申请首页 | 布袋除尘器-单机除尘器-脉冲除尘器-泊头市兴天环保设备有限公司 布袋除尘器|除尘器设备|除尘布袋|除尘设备_诺和环保设备 | 电动卫生级调节阀,电动防爆球阀,电动软密封蝶阀,气动高压球阀,气动对夹蝶阀,气动V型调节球阀-上海川沪阀门有限公司 | 芝麻黑-芝麻黑石材厂家-永峰石业 | 合金耐磨锤头_破碎机锤头_郑州市德勤建材有限公司 | ?水马注水围挡_塑料注水围挡_防撞桶-常州瑞轩水马注水围挡有限公司 | 陶瓷加热器,履带式加热器-吴江市兴达电热设备厂 | 对夹式止回阀厂家,温州对夹式止回阀制造商--永嘉县润丰阀门有限公司 | 陕西高职单招-陕西高职分类考试网 | 上海皓越真空设备有限公司官网-真空炉-真空热压烧结炉-sps放电等离子烧结炉 | 恒压供水控制柜|无负压|一体化泵站控制柜|PLC远程调试|MCGS触摸屏|自动控制方案-联致自控设备 | 制丸机,小型中药制丸机,全自动制丸机价格-甘肃恒跃制药设备有限公司 | Brotu | 关注AI,Web3.0,VR/AR,GPT,元宇宙区块链数字产业 | 退火炉,燃气退火炉,燃气热处理炉生产厂家-丹阳市丰泰工业炉有限公司 | 集装箱标准养护室-集装箱移动式养护室-广州璟业试验仪器有限公司 | 高效复合碳源-多核碳源生产厂家-污水处理反硝化菌种一长隆科技库巴鲁 | 交变/复合盐雾试验箱-高低温冲击试验箱_安奈设备产品供应杭州/江苏南京/安徽马鞍山合肥等全国各地 | 企典软件一站式企业管理平台,可私有、本地化部署!在线CRM客户关系管理系统|移动办公OA管理系统|HR人事管理系统|人力 | 联系我们老街华纳娱乐公司官网19989979996(客服) | 山东聚盛新型材料有限公司-纳米防腐隔热彩铝板和纳米防腐隔热板以及钛锡板、PVDF氟膜板供应商 | 对夹式止回阀厂家,温州对夹式止回阀制造商--永嘉县润丰阀门有限公司 | 信阳网站建设专家-信阳时代网联-【信阳网站建设百度推广优质服务提供商】信阳网站建设|信阳网络公司|信阳网络营销推广 | RFID电子标签厂家-上海尼太普电子有限公司 | 台式低速离心机-脱泡离心机-菌种摇床-常州市万丰仪器制造有限公司 | 干粉砂浆设备_干混砂浆生产线_腻子粉加工设备_石膏抹灰砂浆生产成套设备厂家_干粉混合设备_砂子烘干机--郑州铭将机械设备有限公司 | 胶原检测试剂盒,弹性蛋白检测试剂盒,类克ELISA试剂盒,阿达木单抗ELISA试剂盒-北京群晓科苑生物技术有限公司 |