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

 更新时间:2021年02月03日 11:39:58   作者:青椒1013  
这篇文章主要介绍了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()" rel="external nofollow" >运行 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);

到此这篇关于Springboot+Netty+Websocket实现消息推送实例的文章就介绍到这了,更多相关Springboot Websocket消息推送内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Spring Boot整合web层实现过程详解

    Spring Boot整合web层实现过程详解

    这篇文章主要介绍了Spring Boot整合web层实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • 如何通过RabbitMq实现动态定时任务详解

    如何通过RabbitMq实现动态定时任务详解

    工作中经常会有定时任务的需求,常见的做法可以使用Timer、Quartz、Hangfire等组件,这次想尝试下新的思路,使用RabbitMQ死信队列的机制来实现定时任务,下面这篇文章主要给大家介绍了关于如何通过RabbitMq实现动态定时任务的相关资料,需要的朋友可以参考下
    2022-01-01
  • java线程池工作队列饱和策略代码示例

    java线程池工作队列饱和策略代码示例

    这篇文章主要介绍了java线程池工作队列饱和策略代码示例,涉及线程池的简单介绍,工作队列饱和策略的分析及代码示例,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • Spring IOC简单理解及创建对象的方式

    Spring IOC简单理解及创建对象的方式

    这篇文章主要介绍了Spring IOC简单理解及创建对象的方式,本文通过两种方式给大家介绍创建对象的方法,通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2021-09-09
  • 一文带你彻底搞懂Lambda表达式

    一文带你彻底搞懂Lambda表达式

    这篇文章主要介绍了一文带你彻底搞懂Lambda表达式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • Java基础知识总结之继承

    Java基础知识总结之继承

    这一篇我们来学习面向对象的第二个特征——继承,文中有非常详细的基础知识总结,对正在学习java的小伙伴们很有帮助,需要的朋友可以参考下
    2021-06-06
  • java中final修饰符实例分析

    java中final修饰符实例分析

    本文通过实例向我们展示了java中final修饰符的概念,final修饰的基本变量和引用类型变量的区别。有需要的小伙伴可以参考下
    2014-11-11
  • 适用于Java初学者的学习路线图

    适用于Java初学者的学习路线图

    这篇文章主要介绍了学习Java的路线图的五个必经阶段,还有一些作者的想法分享给大家,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • SpringBoot统计一个Bean中方法的调用次数的实现步骤

    SpringBoot统计一个Bean中方法的调用次数的实现步骤

    这篇文章主要给大家介绍了SpringBoot统计一个Bean中方法的调用次数的实现步骤,文中通过代码示例和图文结合的方式给大家讲解的非常详细,对大家的学习具有一定的帮助,需要的朋友可以参考下
    2024-01-01
  • Spring Boot中操作使用Redis实现详解

    Spring Boot中操作使用Redis实现详解

    Spring Boot与Redis结合使用,通过使用Spring Data Redis来实现对Redis的操作,实现数据缓存和高效存储,提高应用程序的性能和响应速度。可以利用Spring Boot自带的Redis Starter方便地集成和配置Redis
    2023-04-04

最新评论