首页 > 编程语言 >Java21 + SpringBoot3集成WebSocket

Java21 + SpringBoot3集成WebSocket

时间:2024-01-20 13:12:10浏览次数:43  
标签:Java21 websocket userId session SpringBoot3 WebSocket message 连接

目录

前言

近日心血来潮想做一个开源项目,目标是做一款可以适配多端、功能完备的模板工程,包含后台管理系统和前台系统,开发者基于此项目进行裁剪和扩展来完成自己的功能开发。
本项目为前后端分离开发,后端基于Java21SpringBoot3开发,前端提供了vue、angular、react、uniapp、微信小程序等多种脚手架工程。
本文主要介绍项目中如何集成WebSocket实现服务器端与客户端的双向通信。

相关技术简介

什么是WebSocket

WebSocket 是一种网络通信协议。RFC6455 定义了它的通信标准。
http是一种无状态,无连接,单向的应用层协议,它采用了请求/响应模型,通信请求只能由客户端发起,服务端对请求做出应答处理。这样的弊端显然是很大的,只要服务端状态连续变化,客户端就必须实时响应,都是通过javascript与ajax进行轮询,这样显然是非常麻烦的,同时轮询的效率低,非常的浪费资源(http一直打开,一直重复的连接)。
于是就有了WebSocket,它是一种全面双工通讯的网络技术,任意一方都可以建立连接将数据推向另一方,WebSocket只需要建立一次连接,就可以一直保持。

WebSocket的原理

  1. WebSocket约定了一个通信的规范,通过一个握手的机制,客户端和服务器之间能建立一个类似tcp的连接,从而方便它们之间的通信
  2. 在WebSocket出现之前,web交互一般是基于http协议的短连接或者长连接
  3. WebSocket是一种全新的协议,不属于http无状态协议,协议名为"ws"

WebSocket与HTTP协议的关系

WebSocket优点

  1. 减少请求费时费资源:是真正的全双工方式,建立连接后,服务器与客户端时完全对等的,可以相互请求,减少了不必要的网络请求时间损耗和网络流量;
  2. 更持久:WebSocket协议通过第一个request建立TCP连接后,只要不主动关闭,就能一直保持连接状态交换数据;
  3. 服务端可以主动向客户端发送消息;

WebSocket应用场景

社交聊天、弹幕、多玩家游戏、协同编辑、股票基金实时报价、体育实况更新、视频会议/聊天、基于位置的应用、在线教育、智能家居等需要高实时的场景都可以使用WebSocket技术实现。

实现方式

本项目后端基于Java 21SpringBoot3开发,前端基于Vue3实现。

添加maven依赖

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

添加WebSocket配置类,定义ServerEndpointExporter Bean

@Configuration
@EnableWebSocket
public class WebSocketConfig {
    /**
     * 注入ServerEndpointExporter,
     * 这个bean会自动注册使用了@ServerEndpoint注解声明的WebSocket Endpoint
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}

定义WebSocket Endpoint

/**
 * 消息提醒计数WebSocket
 */
@ServerEndpoint("/ws/test/{userId}")
@Component
@Slf4j
public class TestWebSocketServer {
    /**
     * 与某个客户端的连接会话,需要通过它来给客户端发送数据
     */
    private Session session;
    /**
     * 用户ID
     */
    private Long userId;
    /**
     * concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
     * 虽然@Component默认是单例模式的,但springboot还是会为每个websocket连接初始化一个bean,所以可以用一个静态set保存起来。
     */
    private static final CopyOnWriteArraySet<MessageCountWebSocketServer> webSockets = new CopyOnWriteArraySet<>();
    /**
     * 用来存在线连接用户信息
     */
    private static final ConcurrentHashMap<Long, Session> sessionPool = new ConcurrentHashMap<>();

    /**
     * 链接成功调用的方法
     */
    @OnOpen
    public void onOpen(Session session, @PathParam(value = "userId") Long userId) {
        this.session = session;
        this.userId = userId;
        webSockets.add(this);
        sessionPool.put(userId, session);
        log.info("建立与UserID:{}的消息提醒计数连接", userId);
    }

    /**
     * 链接关闭调用的方法
     */
    @OnClose
    public void onClose() {
        webSockets.remove(this);
        sessionPool.remove(this.userId);
        log.info("关闭与UserID:{}的消息提醒计数连接", userId);
    }

    /**
     * 收到客户端消息后调用的方法
     */
    @OnMessage
    public void onMessage(String message) {
        log.info("接收到UserID:{}的消息{}", userId, message);
    }

    /**
     * 发送错误时的处理
     */
    @OnError
    public void one rror(Session session, Throwable error) {
        log.error("发送到UserID:{}的消息传输失败", userId, error);
    }

    /**
     * 广播消息
     *
     * @param message
     */
    public void sendAllMessage(String message) {
        for (MessageCountWebSocketServer socketServer : webSockets) {
            if (socketServer.session.isOpen()) {
                socketServer.session.getAsyncRemote().sendText(message);
            }
        }
    }

    /**
     * 单人单播消息
     *
     * @param userId
     * @param message
     */
    public void sendOneMessage(Long userId, String message) {
        Session session = sessionPool.get(userId);
        if (session != null && session.isOpen()) {
            session.getAsyncRemote().sendText(message);
        }
    }

    /**
     * 多人单播消息
     *
     * @param userIds
     * @param message
     */
    public void sendMoreMessage(Long[] userIds, String message) {
        for (Long userId : userIds) {
            Session session = sessionPool.get(userId);
            if (session != null && session.isOpen()) {
                session.getAsyncRemote().sendText(message);
            }
        }

    }
}

前端创建WebSocket对象

以下代码基于Vue3的组合式API编写。

<script setup>
  import { onMounted, onBeforeMount } from 'vue';

  /**
   * @type {WebSocket}
   */
  let websocket = null;

  onMounted(async () => {
    initTestWebSocket();
  });

  onBeforeMount(async()=>{
    websocket && websocket.close();
  });

  const initTestWebSocket = async () => {
    const userId = '当前用户ID';
    console.log("尝试建立websockect连接");
    websocket = new WebSocket(`/ws/test/${userId}`);
    websocket.onopen = function (event) {
      console.log("建立连接");
    }
    websocket.onclose = function (event) {
      console.log('连接关闭')
      //尝试重连websocket
      reconnectMessageWebSocket();
    }
    //建立通信后,监听到后端的数据传递
    websocket.onmessage = function (event) {
      // 打印后端传来的数据
      console.log(event.data);
      // 调用WebSocket对象的send方法可向后端发送数据
      // websocket.send("test data");
    }
    websocket.onerror = function () {
      console.log("数据发送失败");
    }
    // 窗口关闭前关闭WebSocket连接
    window.onbeforeunload = function () {
      websocket.close();
    }
  };

  // 重连
  const reconnectMessageWebSocket = () => {
    console.log("正在重连");
    // 进行重连
    setTimeout(() => {
      initTestWebSocket();
    }, 1000);
  }
</script>

总结

本文介绍了WebSocket的相关概念,以及如何基于Java21、SpringBoot3和Vue3使用WebSocket,在使用过程中也遇到了一些问题。

  • 执行mvn packagemvn test命令时报错

请参阅 Java21 + SpringBoot3使用spring-websocket时执行mvn package报错

  • 如果后端使用Spring SecurityShiroSa-Token等技术,需要考虑使用@ServerEndpoint注解所配置url的权限控制问题。

我也会及时的更新后续实践中所遇到的问题,希望与诸位看官一起进步。
如有错误,还望批评指正。

标签:Java21,websocket,userId,session,SpringBoot3,WebSocket,message,连接
From: https://www.cnblogs.com/breezefaith/p/17976343

相关文章

  • hyperexpress框架/使用uwebsockets.js核心
    import{scheduleJob}from'node-schedule';//定时任务functionsetupScheduledTasks(){//每6秒执行一次setInterval(()=>{taskEverySixSeconds();},6000);//每33分钟执行一次setInterval(()=>{taskEve......
  • uWebSockets.js 框架经验
    目录结构project/│├──src/│├──app.ts│├──routes/││├──userRoutes.ts││└──index.ts│├──entities/││└──User.ts│├──utils/││└──parseQuery......
  • 42 干货系列从零用Rust编写负载均衡及代理,wmproxy中配置tcp转websocket
    wmproxywmproxy已用Rust实现http/https代理,socks5代理,反向代理,静态文件服务器,四层TCP/UDP转发,七层负载均衡,内网穿透,后续将实现websocket代理等,会将实现过程分享出来,感兴趣的可以一起造个轮子项目地址国内:https://gitee.com/tickbh/wmproxygithub:https://github.com/......
  • C# websocket服务端实现
    1、创建一个winform项目2、创建websocket服务端类WebSocket_Service.cs1usingSystem;2usingSystem.Collections.Generic;3usingSystem.ComponentModel;4usingSystem.Linq;5usingSystem.Net;6usingSystem.Net.Sockets;7usingSystem.Security......
  • Springboot3+Vue3在进行WebSocket通讯时出现No mapping for GET或者是404
    参考:在SpringBoot中整合、使用WebSocket-spring中文网(springdoc.cn)===============================原代码(此时前端访问后端,后端会出现:NomappingforGET/wspath)前端相关代码:letsocket:WebSocket|null=nullconstsocketURL=`ws://127.0.0.1:8084/w......
  • docker jmeter分布式压测部署 jmeter websocket压测
    测试场景:1.多名用户加入房间。2.房间人数为固定人数(比如4人) 3.有人进入时,进入用户会收到反馈当前房间人员列表。4.其他人会收到反馈新加入用户的信息消息。5.当人数已满时,会自动推送消息给所有人。6.在人满后,每个人需要按固定序列,发送消息。7.所有人发送特定消息后,推进房......
  • JMeter测试WebSocket的经验总结
    最近有一个微信聊天系统的项目需要性能测试,既然是测试微信聊天,肯定绕不开websocket接口的测试,首选工具是Jmeter,网上能搜到现成的方法,但是网上提供的jar包往往不是最新的,既然是用最新版本的Jmeter4.0,那么所依赖的插件jar包也应该追求新的。所以提供了以下链接供大家下载(甚至连源码......
  • Django中安装websocket
    完整代码:https://gitee.com/mom925/django-system项目结构:先安装所需库:pipinstallchannels下面将websocket作为插件一样的只需要引入配置的结构asgi.py文件http请求不变、修改websocket请求调用路径importosimportdjangofromchannels.httpimportAsgiHandlerfr......
  • 41. 干货系列从零用Rust编写负载均衡及代理,websocket与tcp的映射,WS与TCP互转
    wmproxywmproxy已用Rust实现http/https代理,socks5代理,反向代理,静态文件服务器,四层TCP/UDP转发,七层负载均衡,内网穿透,后续将实现websocket代理等,会将实现过程分享出来,感兴趣的可以一起造个轮子项目地址国内:https://gitee.com/tickbh/wmproxygithub:https://github.com/......
  • Go Websocket库推荐
    gws常用的操作json格式参考homeassiatant文档中的那个定义:hawebsocket文档定义handler,它是gws的websocket的回调方法集合定义的接口//ClientEventHandler是Websocket事件回调的模板.//有open,close,ping,pong(客户端其实没有ping操作,所以就自然不存在pong......