Springboot+Netty+Websocket实现消息推送实例

2023-11-15

Springboot+Netty+Websocket实现消息推送



前言

WebSocket 使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。在 WebSocket API 中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接,并进行双向数据传输。
Netty框架的优势

 1. API使用简单,开发门槛低;
 2. 功能强大,预置了多种编解码功能,支持多种主流协议;
 3. 定制能力强,可以通过ChannelHandler对通信框架进行灵活地扩展;
 4. 性能高,通过与其他业界主流的NIO框架对比,Netty的综合性能最优;
 5. 成熟、稳定,Netty修复了已经发现的所有JDK NIO BUG,业务开发人员不需要再为NIO的BUG而烦恼

提示:以下是本篇文章正文内容,下面案例可供参考

一、引入netty依赖

<dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.48.Final</version>
</dependency>

二、使用步骤

1.引入基础配置类

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服务启动监听器

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>
 * 服务启动监听器
 **/
@Slf4j
@Component
public class NettyServer {

    @Value("${server.netty.port}")
    private int port;

    @Autowired
    private ServerChannelInitializer serverChannelInitializer;

    @Bean
    ApplicationRunner nettyRunner() {
        return args -> {
            //new 一个主线程组
            EventLoopGroup bossGroup = new NioEventLoopGroup(1);
            //new 一个工作线程组
            EventLoopGroup workGroup = new NioEventLoopGroup();
            ServerBootstrap bootstrap = new ServerBootstrap()
                    .group(bossGroup, workGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(serverChannelInitializer)
                    //设置队列大小
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    // 两小时内没有数据的通信时,TCP会自动发送一个活动探测数据报文
                    .childOption(ChannelOption.SO_KEEPALIVE, true);
            //绑定端口,开始接收进来的连接
            try {
                ChannelFuture future = bootstrap.bind(port).sync();
                log.info("服务器启动开始监听端口: {}", port);
                future.channel().closeFuture().sync();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                //关闭主线程组
                bossGroup.shutdownGracefully();
                //关闭工作线程组
                workGroup.shutdownGracefully();
            }
        };
    }
}

3.netty服务端处理器

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服务端处理器
 **/
@Slf4j
@Component
@ChannelHandler.Sharable
public class NettyServerHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {

    @Autowired
    private ServerChannelCache cache;
    private static final String dataKey = "test=";

    @Data
    public static class ChannelCache {
    }


    /**
     * 客户端连接会触发
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        log.info("通道连接已打开,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("通道连接已断开,ID->{},用户ID->{}......", channel.id().asLongText(), cache.getCacheId(channel));
        cache.remove(channel);
    }

    /**
     * 发生异常触发
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        Channel channel = ctx.channel();
        log.error("连接出现异常,ID->{},用户ID->{},异常->{}......", channel.id().asLongText(), cache.getCacheId(channel), cause.getMessage(), cause);
        cache.remove(channel);
        ctx.close();
    }

    /**
     * 客户端发消息会触发
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
        try {
            // log.info("接收到客户端发送的消息:{}", 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("服务器下发消息: {}", 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("服务器下发消息: {}", msg);
        channel.writeAndFlush(new TextWebSocketFrame(msg));
    }

}

4.netty服务端缓存类

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;

@Component
public 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服务初始化器

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服务初始化器
 **/
@Component
public 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测试

<!DOCTYPE HTML>
<html>
   <head>
   <meta charset="utf-8">
   <title>test</title>
    
      <script type="text/javascript">
         function WebSocketTest()
         {
            if ("WebSocket" in window)
            {
               alert("您的浏览器支持 WebSocket!");
               
               // 打开一个 web socket
               var ws = new WebSocket("ws://localhost:port/test.io");
                
               ws.onopen = function()
               {
                  // Web Socket 已连接上,使用 send() 方法发送数据
                  ws.send("发送数据");
                  alert("数据发送中...");
               };
                
               ws.onmessage = function (evt) 
               { 
                  var received_msg = evt.data;
                  alert("数据已接收...");
               };
                
               ws.onclose = function()
               { 
                  // 关闭 websocket
                  alert("连接已关闭..."); 
               };
            }
            
            else
            {
               // 浏览器不支持 WebSocket
               alert("您的浏览器不支持 WebSocket!");
            }
         }
      </script>
        
   </head>
   <body>
   
      <div id="sse">
         <a href="javascript:WebSocketTest()">运行 WebSocket</a>
      </div>
      
   </body>
</html>

7.vue测试

 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: "建立实时连接成功!",
                                duration: 1000
                            })
                            setInterval(()=>{websocket.send("heartbeat")},60*1000);
                            break;
                        case "001":
                            this.$message.warning("收到一条新的信息,请及时查看!")
                            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.服务器下发消息

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

总结

按照上面步骤,一步一步的来,是可以实现消息推送的功能的。不要着急,当你调试成功之后,发现并没有自己想想中的那么难,加油!亲测可以使用,如果感觉本文档对您有帮助,可以请喝个下午茶!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Springboot+Netty+Websocket实现消息推送实例 的相关文章

随机推荐

  • markdown自动生成导航目录

    把这一段代码插入到markdown生成的HTML文件的head标签中 将会自动根据markdown的标题按级别生成导航目录
  • 可能是Windows下最简单的Java环境安装指南

    1 简介 JDK Java SE Development Kit Java开发工具 JRE Java Runtime Environment Java运行环境 如果要从事Java编程 则需要安装JDK 如果仅仅是运行一款Java程序则JRE
  • Keycloak配置模拟用户登录

    提示 文章写完后 目录可以自动生成 如何生成可参考右边的帮助文档 文章目录 一 keycloak配置 一 keycloak配置 keycloak启动 winodws通过配置项启动keycloak 进入keycloak安装目录keycloak
  • 基于Qt的OpenGL编程(3.x以上GLSL可编程管线版)---(二十四)高级GLSL技巧

    Vries的教程是我看过的最好的可编程管线OpenGL教程 没有之一 其原地址如下 https learnopengl cn github io 04 20Advanced 20OpenGL 08 20Advanced 20GLSL 关于高
  • OLED\OLED.axf: Error: L6200E: Symbol __asm___6_oled_c_F16x16____REV16 multiply defined (by oled_1.o

    关于这个问题的解决我当时找了很久不知道该如何解决它总说重复最后发现应该是当时在别的文件夹引入的时候重复了 选择Remove Group然后重新编译 就ok了
  • 迁移学习(transfer learning)和微调(fine-tune)的几点认识

    迁移学习和微调的区别 什么是迁移学习 即一种学习对另一种学习的影响 它广泛地存在于知识 技能 态度和行为规范的学习中 任何一种学习都要受到学习者已有知识经验 技能 态度等的影响 只要有学习 就有迁移 迁移是学习的继续和巩固 又是提高和深化学
  • java实现多图片或多图片上传

    文章目录 一 单图上传 步骤 前台页面 后台实现文件上传的操作 二 多图上传 多图上传 还需要多一些步骤 适用功能 一 单图上传 步骤 前台页面 1 文件样式世界 div class layui input block img src al
  • Vue【四】vue自定义指令。

    Vue 四 vue自定义指令 文章目录 Vue 四 vue自定义指令 TOC 文章目录 前言 一 自定义指令 局部注册 二 自定义指令 全局注册 三 自定义指令 指令的值 总结 前言 除了核心功能默认内置的指令 v model 和 v sh
  • 查询SQL表占用空间(sp_spaceused 表名)

    create table tablespaceinfo 狦 nameinfo varchar 50 rowsinfo int reserved varchar 20 datainfo varchar 20 index size varcha
  • 保定2021高考学校成绩查询,2021年保定高考成绩排名及成绩公布时间什么时候出来...

    保定高考结束后 每年都有很多家长和考试不知道保定高考成绩排名如何查询 保定高考成绩什么时候公布以及查询方式 本文小编整理了保定高考成绩查询排名的相关知识 一 保定高考成绩公布时间及查询方式 根据往年保定高考成绩公布时间预测 2021年保定高
  • 小程序授权微信登录,获取微信用户名,头像,登录code

    getUserProfile e var that this wx getUserProfile desc 用于完善会员资料 success res gt if res 用户名 res userInfo nickName 头像res use
  • 【BERT类预训练模型整理】

    BERT类预训练模型整理 1 BERT的相关内容 1 1 BERT的预训练技术 1 1 1 掩码机制 1 1 2 NSP Next Sentence Prediction 1 2 BERT模型的局限性 2 RoBERTa的相关内容 2 1
  • 小程序(二十三)微信小程序左上角返回按钮触发事件

    微信并没有为我们提供左上角返回上一页触发的事件 但是有的时候这个操作我们还是需要监听一下 下图红框标注的返回上一页按钮 最后实现的效果 点击返回上一页的时候 我需要重新加载上一页的数据 返回上一页按钮只会触发上一页的onShow生命周期函数
  • 怎么将本地文件上传到远程git仓库

    怎么将本地文件上传到远程git仓库 1 先进入项目文件夹 通过命令 git init 把这个目录变成git可以管理的仓库 git init 2 把文件添加到版本库中 使用命令 git add 添加到暂存区里面去 不要忘记后面的小数点 意为添
  • 第二篇:UE4如何动态修改物体材质

    1 找到需要替换材质的物体 可以看到下方所有的材质 它是一个数组 前面是材质的下标 2 打开关卡蓝图 编写如下蓝图 第一步获取所有材质 创建动态材质接口 第二部设置材质 element Index代表要替换的材质下标 Material代表要
  • 米米商城项目

    米米商城 1 开发环境 2 项目功能 3 项目搭建步骤 4 配置文件 4 1 pom xml 4 2 jdbc properties 4 3 Mybaties配置文件 SqlMapConfig xml 4 4 Spring配置文件 4 4
  • 15.Mybatis 更新操作-update

    1 update 标签 update 标签是用于定义更新 语句的 1 1 常用属性 update 有几个常用的属性 但是通常只需要设置id 即可 id sql 片段在命名空间内的唯一标识 和mapper 中方法名保持一致 parameter
  • python使用keyboard库写的GUI键盘宏

    前言 之前和朋友玩游戏 需要一直按住两个按键 很麻烦 就像用python写个小脚本来方便自己 说干就干 用于学习 正文 用到的库 keyborad threading tkinter time 分析 由于需要监听键盘与运行可视化界面 所以要
  • 5.1 setfenv,但5.3可以使用lua_getglobal(l1, "_G");

    5 1使用法 lua State L luaL newstate luaL openlibs L dostring L function f1 my var 100 print var set end create func on stat
  • Springboot+Netty+Websocket实现消息推送实例

    Springboot Netty Websocket实现消息推送 文章目录 Springboot Netty Websocket实现消息推送 前言 一 引入netty依赖 二 使用步骤 1 引入基础配置类 2 netty服务启动监听器 3