springboot使用websocket(undertow容器)

背景

在很多实时性需求比较高的场景,例如k线,盘口,用户余额这些场景,我们使用的一般就是两种方式获取数据,前端轮询查询接口或者使用websocket来进行数据获取。

引入依赖

这里就没有把依赖包管理贴出来了,版本自行选择

		<!-- 引入web依赖,排除tomcat -->
		 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 引入undertow容器 -->
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
        <!-- 引入websocket依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
  • 为啥不使用tomcat呢,因为它对ws的支持不是很好,总会出现一些问题
  • 为啥不用jetty呢,如果我们要使用controller等,和tomcat的差别比较大,不易更改,如果我们同时需要使用websocket与http,那么建议还是使用undertow
    其实这些都不重要,主要就是undertow的ws支持比较好,编码和tomcat差别不大,容易上手

配置编写

1、自定义返回序列化的类

import org.springframework.context.annotation.Configuration;

import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;

/**
* 这里的ApiResult是我自定义的响应体,可更换为自己的响应体
* @author : yyds
* @date : 2023/12/4 16:35
*/
@Configuration
public class WebSocketEncoder implements Encoder.Text<ApiResult> {
   @Override
   public String encode(ApiResult apiResult) throws EncodeException {
       // 定义自己的格式化方式
       return null;
   }

   @Override
   public void init(EndpointConfig endpointConfig) {

   }

   @Override
   public void destroy() {

   }
}

2、注入bean

随便在一个类里面注入即可,socket客户端

   @Bean
   public ServerEndpointExporter serverEndpointExporter() {
       return new ServerEndpointExporter();
   }
   
   // 解决spring中socketBean冲突,如果有冲突就加没有就不管
   @Bean
   @Nullable
   public TaskScheduler taskScheduler() {
       ThreadPoolTaskScheduler threadPoolScheduler = new ThreadPoolTaskScheduler();
//        threadPoolScheduler.setThreadNamePrefix("SockJS-");
//        threadPoolScheduler.setPoolSize(Runtime.getRuntime().availableProcessors());
//        threadPoolScheduler.setRemoveOnCancelPolicy(true);
       return threadPoolScheduler;
   }

3、实现客户端连接

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author yyds
 * @date 2023/11/15
 */
@Data
@Slf4j
@Component
@ServerEndpoint(value = "/ws/{userId}",encoders = {WebSocketEncoder.class})
public class WebSocketEndPoint {

    /**
     * 用户ID
     */
    private String userId;

    /**
     * 当前客户端的连接会话, 需要通过它来给客户端发送消息
     */
    private Session session;

    /**
     * WS 会话连接池
     */
    private static ConcurrentHashMap<String, Session> sessionPool = MapUtil.newConcurrentHashMap();

    public static ConcurrentHashMap<String, Session> getSessionPool() {
        return sessionPool;
    }

    private String getSessionId(String userId, Session session) {
        return userId + StrUtil.AT + session.getId();
    }
    @OnOpen
    public void onOpen(Session session, @PathParam(value = "userId") String userId) {
        this.userId = userId;
        this.session = session;
        sessionPool.put(getSessionId(userId, session), session);
        log.info("【ws-connect】sessionId:{}, userId:{}, 连接总数:{}", session.getId(), userId, sessionPool.size());
        try {
            sendMessageToOne(userId, WebSocketMessageEnum.CONNECT_SUCCESS.getCode());
        } catch (Exception ex) {
            log.error("【ws】发送连接成功消息异常, userId=" + userId, ex);
        }
    }

    @OnClose
    public void onClose(CloseReason close) {
        sessionPool.remove(getSessionId(userId, session));
        CloseReason.CloseCode closeCode = close.getCloseCode();
        log.info("【ws-close】userId:{}断开,断开code: {}, 是否正常断开:{} 连接总数:{}", this.userId, closeCode, closeCode == CloseReason.CloseCodes.NORMAL_CLOSURE, sessionPool.size());
    }

    @OnMessage
    public void onMessage(String message) {
        if (StrUtil.equals(message, WebSocketMessageEnum.HEALTH_CHECK_PING.getCode())) {
            log.info("【ws-message】收到用户[{}]客户端心跳检测消息:{}", this.userId, message);
            sendMessageToOne(this.userId, WebSocketMessageEnum.HEALTH_CHECK_PONG.getCode());
        } else {
            log.info("【ws-message】收到用户[{}]客户端发来的消息:{}", this.userId, message);
        }
    }

    @OnError
    public void onError(Session session, Throwable throwable) {
        log.error("【ws消息】错误" + this.userId, throwable);
    }

    /**
     * 广播消息
     *
     * @param message 消息内容
     */
    public void sendMessageToAll(String message) {
        // log.info("【ws消息】广播消息, 广播人数:{}, message={}", sessionPool.size(), message);
        for (Map.Entry<String, Session> entry : sessionPool.entrySet()) {
            Session session = entry.getValue();
            if (ObjectUtil.isNotNull(session) && session.isOpen()) {
                try {
                    session.getAsyncRemote().sendText(message);
                } catch (Exception ex) {
                    log.error("【ws广播消息错误】", ex);
                }
            }
        }
    }

    /**
     * 单点消息(单人)
     *
     * @param userId  用户ID
     * @param message 消息内容
     */
    public void sendMessageToOne(String userId, String message) {
        log.info("【ws-message-one】userId:{}, message:{}", userId, message);
        Session session = sessionPool.get(userId);
        if (ObjectUtil.isNotNull(session) && session.isOpen()) {
            try {
                session.getAsyncRemote().sendText(message);
            } catch (Exception ex) {
                log.error("【ws单点消息错误】", ex);
            }
        }
    }

    /**
     * 获取在线人数
     */
    public Integer onlineUsers() {
        return sessionPool.size();
    }
}

至此,websocket就已经就可以连接了。

注意

  • 前端同时发送多笔消息的时候,可能会出现问题,建议是收到消息回复后在进行下一步,或者初始化信息的时候,调用接口进行处理
  • 建议采用字符串形式传送,并且添加开始和结束标识符比如 消息内容
  • 前后端都做心跳检测
  • 记得在报错或者其他情况下需要关闭连接,并且移出缓存
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot 框架支持使用 WebSocket 协议进行通信。可以使用 Spring Boot 的内置支持来配置 WebSocket,也可以使用第三方库如 Spring WebSocket 来实现 WebSocket 功能。使用 Spring Boot 可以简化 WebSocket 的配置和使用。 ### 回答2: SpringBoot是一个非常流行的Java web框架,而WebSocket则是一种全新的web通信协议,通过 WebSocket 可以实现 Web 页面与服务器的实时双向通信。SpringBootWebsocket模块提供了强大的支持,使得使用 WebSocket 开发项目变得更加容易。 在springboot使用websocket,首先需要引入SpringWebSocket模块以及对应的STOMP协议的依赖,可以通过在pom.xml中添加以下依赖实现: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> ``` 在Spring Boot应用程序中使用WebSocket非常容易,我们可以分为以下几个步骤: 1. 创建一个WebSocket配置类 在WebSocket配置类中,需要添加@EnableWebSocketMessageBroker和@Configuration注解。其中,@EnableWebSocketMessageBroker注解表示开启WebSocket消息代理支持,@Configuration注解表示该类为配置类。 ``` @Configuration @EnableWebSocketMessageBroker public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { // TODO 添加相关配置 } ``` 2. 配置WebSocket消息代理 在WebSocketConfig中,需要重写configureMessageBroker方法,来配置WebSocket消息代理。在该方法中,需要首先调用enableSimpleBroker方法,它用于配置简单的消息代理,使得订阅者能够订阅消息。 ``` @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker("/topic"); } ``` 3. 配置WebSocket请求路径 在WebSocketConfig中,需要重写registerStompEndpoints方法,来配置WebSocket请求路径。该方法中,需要调用addEndpoint方法,来注册一个WebSocket端点(/chat),供客户端访问。 ``` @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/chat").withSockJS(); } ``` 4. 编写WebSocket消息处理类 编写一个WebSocket消息处理类,用来处理WebSocket相关的消息和事件。需要在类上添加@Controller注解,同时在方法上添加@MessageMapping注解,用来指定方法处理的消息地址。 ``` @Controller public class ChatController { @MessageMapping("/msg") @SendTo("/topic/messages") public ChatMessage sendMessage(@Payload ChatMessage chatMessage) { return chatMessage; } } ``` 5. 编写WebSocket客户端代码 最后,我们需要编写WebSocket客户端代码来测试WebSocket通信。在客户端代码中,需要先建立与服务器的连接,然后通过WebSocket发送和接收消息。 ``` var socket = new SockJS('/chat'); var stompClient = Stomp.over(socket); stompClient.connect({}, function (frame) { stompClient.subscribe('/topic/messages', function (chatMessage) { console.log(chatMessage); }); }); function sendMessage() { var message = {sender: 'Alice', content: 'Hello!'}; stompClient.send("/app/msg", {}, JSON.stringify(message)); } ``` 以上就是使用SpringBoot开发WebSocket的主要步骤,通过这些步骤,我们可以轻松地开发出基于WebSocket的实时通讯功能。总体来说,SpringBootWebSocket模块提供了强大的支持,使得使用WebSocket开发项目变得更加容易。 ### 回答3: Springboot是一个开源的Java框架,用于创建可扩展的基于RESTful的Web应用程序。其中,Websocket是一种协议,使用在客户端和服务端之间进行实时通信。在Springboot使用Websocket可以用于实现即时通信、聊天、实时数据交换等功能。 Springboot使用Websocket需要两个方面:客户端代码和服务端代码。在客户端的代码中,需要引入一个websocket库,例如SockJS或Stomp;在服务端的代码中,则需要使用@ServerEndpoint注解,并为Websocket注册处理类。 Websocket使用的过程中,客户端会建立与服务端的一条持久连接,以进行数据交换。在客户端与服务端建立连接之后,可以发送消息到服务端并通过该连接接收来自服务端的消息。 SpringbootWebsocket还支持多个连接的概念,即多个客户端可以与服务端建立多个连接,之间不会相互干扰。 在Springboot使用Websocket的优点是它提供了一个简便易用的方式来实现低延迟的双向通信,同时又减少了对HTTP请求的频繁使用,从而提升Web应用的性能和效率。 总之,Springboot使用Websocket是一种方便实用的方式,可以省略掉复杂的代码和步骤,同时还可以快速开发多种实时通信功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值