Springboot Vue WebSocket实现后端服务推送通知功能

14 篇文章 0 订阅
3 篇文章 0 订阅

一、业务场景

最近在做一个功能,业务待办事件数量实时推送给前端页面,最初的设想是在前端定时调用后端接口获取,但是这样就无法实现实时信息,所以,就想到了WebSocket。废话不多说,上菜。。。

二、实现过程

Springboot 和 Vue项目的构建我就不赘述了,老生常谈。

1. Springboot pom.xml引入WebSocket依赖jar包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
    <version>2.2.5.RELEASE</version>
</dependency>

2. 编写WebSocketConfig配置类

@Configuration
public class WebSocketConfig {

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

}

3. 编写WebSocketServer服务实现类

@Component
@ServerEndpoint("/push/websocket")
@Slf4j
@EqualsAndHashCode
public class WebSocketServer {
    /**
     * 静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
     */
    private static int onlineCount = 0;
    /**
     * concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
     */
    private final static CopyOnWriteArraySet<WebSocketServer> WEB_SOCKET_SET = new CopyOnWriteArraySet<>();

    /**
     * 与某个客户端的连接会话,需要通过它来给客户端发送数据
     */
    private Session session;

    /**
     * 接收sid
     */
    private String sid = "";

    /**
     * 连接建立成功调用的方法
     */
    @OnOpen
    public void onOpen(Session session, @PathParam("sid") String sid) throws IOException {
        this.session = session;
        // 加入set中
        WEB_SOCKET_SET.add(this);
        // 在线数加1
        addOnlineCount();
        log.info("有新客户端连接:" + sid + ",当前在线人数为" + getOnlineCount());
        this.sid = sid;
        // TODO 简历连接成功后可以直接发送消息
    }

    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose() {
        //从set中删除
        WEB_SOCKET_SET.remove(this);
        // 在线数减1
        subOnlineCount();
        log.info("有一个客户端连接关闭!当前在线人数为" + getOnlineCount());
    }

    /**
     * 收到客户端消息后调用的方法
     *
     * @param message 客户端发送过来的消息
     */
    @OnMessage
    public void onMessage(String message, Session session) {
        log.info("收到来自窗口" + sid + "的信息:" + message);
        System.out.println(session);
    }

    /**
     * @param session session
     * @param e       异常信息
     */
    @OnError
    public void onError(Session session, Throwable e) {
        log.error("发生错误");
        System.out.println(session);
        e.printStackTrace();
    }

    /**
     * 实现服务器主动推送
     */
    public void sendMessage(String message) throws IOException {
        this.session.getBasicRemote().sendText(message);
    }

    /**
     * 群发自定义消息
     */
    public void sendInfo(String message) throws IOException {
        for (WebSocketServer item : WEB_SOCKET_SET) {
            item.sendMessage(message);
            log.info("推送信息内容=>{}", message);
        }
    }

    public static synchronized int getOnlineCount() {
        return onlineCount;
    }

    public static synchronized void addOnlineCount() {
        WebSocketServer.onlineCount++;
    }

    public static synchronized void subOnlineCount() {
        WebSocketServer.onlineCount--;
    }

}

4. 编写一个Controller接口用于调用(真正业务上的调用方式应该是数据改变时调用发送消息,这里用于测试)

@Slf4j
@RestController
@RequestMapping("/websocket")
public class WebSocketController {

	private final WebSocketServer webSocketServer;

	public WebSocketController (WebSocketServer webSocketServer) {
        this.webSocketServer= webSocketServer;
    }

    @GetMapping("test")
    public void test() {
        try {
            webSocketServer.sendInfo("后端服务推送信息");
        } catch (Exception e) {
            log.error("分页查询数据接口列表失败", e);
        }
    }

}

5. 编写Vue前端代码配置

export default {
  name: "Asider",
  data() {
    return {
      websocket: null
    }
  },
  methods: {
    initWebSocket() {
      // 连接错误
      this.websocket.onerror = this.setErrorMessage
      // 连接成功
      this.websocket.onopen = this.setOnopenMessage
      // 收到消息的回调
      this.websocket.onmessage = this.setOnmessageMessage
      // 连接关闭的回调
      this.websocket.onclose = this.setOncloseMessage
      // 监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
      window.onbeforeunload = this.onbeforeunload
    },
    setErrorMessage() {
      window.console.log('WebSocket连接发生错误,状态码:' + this.websocket.readyState)
    },
    setOnopenMessage() {
      window.console.log('WebSocket连接成功,状态码:' + this.websocket.readyState)
    },
    setOnmessageMessage(event) {
      // 根据服务器推送的消息做自己的业务处理
      window.console.log(event.data);
    },
    setOncloseMessage() {
      window.console.log('WebSocket连接关闭,状态码:' + this.websocket.readyState)
    },
    onbeforeunload() {
      this.closeWebSocket()
    },
    closeWebSocket() {
      this.websocket.close()
    }
  },
  mounted() {
    if ('WebSocket' in window) {
      this.websocket = new WebSocket('ws://localhost:8080/push/websocket');
      this.initWebSocket();
    } else {
      alert('当前浏览器不支持WebSocket!!!')
    }
  }
}

三、结语

编写完成,欢迎一键三连。

  • 2
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Spring Boot和Vue可以通过Websocket实现后端的主动推送功能。 首先,在Spring Boot中,我们需要引入WebSocket的支持。可以在pom.xml文件中添加Spring Boot的WebSocket依赖。然后,创建一个WebSocket配置类,并添加注解@EnableWebSocket,用于启用WebSocket。 接下来,在配置类中,我们需要注册一个WebSocket处理器,并设置处理器的路径。可以使用@WebSocketMapping注解来指定处理器的路径,同时通过实现WebSocketHandler接口来处理WebSocket消息。 在处理器中,我们可以重写父类的方法,例如onOpen()、onMessage()、onClose()等,来处理WebSocket的事件,如连接建立、收到消息、连接关闭等。 然后,在Vue中,我们可以使用Vue.js的插件vue-native-websocket来连接WebSocket。首先,可以使用npm安装vue-native-websocket插件。然后,在main.js文件中,引入插件并进行相关配置。 在Vue组件中,可以使用this.$socket来访问WebSocket连接,并通过监听连接的事件,如open、message、close等,来处理WebSocket的事件。 在进行WebSocket连接后,后端可以在需要的时候,主动向前端发送消息。可以使用WebSocket的sendMessage()方法,将消息发送给所有连接的WebSocket客户端。 总结起来,Spring Boot和Vue可以通过WebSocket实现后端的主动推送功能。在Spring Boot中,需要通过WebSocket配置类来注册WebSocket处理器,并在处理器中处理WebSocket的事件。在Vue中,可以使用vue-native-websocket插件来连接WebSocket,并通过监听事件来处理WebSocket的事件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZwLemon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值