SpringBoot 使用WebSocket详解

后端

1、导入websocket包

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

2、启动类配置,配置Bean

@SpringBootApplication
public class SimCardFeesApplication {
    public static void main(String[] args) {
        SpringApplication.run(SimCardFeesApplication.class, args);
    }

    /**
     * 会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint
     * 要注意,如果使用独立的servlet容器,
     * 而不是直接使用springboot的内置容器,
     * 就不要注入ServerEndpointExporter,因为它将由容器自己提供和管理。
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}

3、Websocket工具类,记录当前在线的链接对链接进行操作

import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WebsocketUtil {
  /**
   * 记录当前在线的Session
   */
  private static final Map<String, Session> ONLINE_SESSION = new ConcurrentHashMap<>();

  /**
   * 添加session
   * @param userId
   * @param session
   */
  public static void addSession(String userId, Session session){
    // 此处只允许一个用户的session链接。一个用户的多个连接,我们视为无效。
    ONLINE_SESSION.putIfAbsent ( userId, session );
  }

  /**
   * 关闭session
   * @param userId
   */
  public static void removeSession(String userId){
    ONLINE_SESSION.remove ( userId );
  }

  /**
   * 给单个用户推送消息
   * @param session
   * @param message
   */
  public static void sendMessage(Session session, String message){
    if(session == null){
      return;
    }
    // 同步
    RemoteEndpoint.Async async = session.getAsyncRemote ();
    async.sendText ( message );
  }

  /**
   * 向所有在线人发送消息
   * @param message
   */
  public static void sendMessageForAll(String message) {
    //jdk8 新方法
    ONLINE_SESSION.forEach((sessionId, session) -> sendMessage(session, message));
  }
}

4、控制类

import com.example.simcardfees.utils.WebsocketUtil;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

/**
 * websocket接口处理类
 */
@Component
@ServerEndpoint(value = "/chat/{userId}")
public class WebsocketController {
    /**
     * 连接事件,加入注解
     * @param userId
     * @param session
     */
    @OnOpen
    public void onOpen(@PathParam(value = "userId") String userId, Session session) {
        String message = "[" + userId + "]加入聊天室!!";
        // 添加到session的映射关系中
        WebsocketUtil.addSession(userId, session);
        // 广播通知,某用户上线了
        WebsocketUtil.sendMessageForAll(message);
    }

    /**
     * 连接事件,加入注解
     * 用户断开链接
     *
     * @param userId
     * @param session
     */
    @OnClose
    public void onClose(@PathParam(value = "userId") String userId, Session session) {
        String message = "[" + userId + "]退出了聊天室...";
        // 删除映射关系
        WebsocketUtil.removeSession(userId);
        // 广播通知,用户下线了
        WebsocketUtil.sendMessageForAll(message);
    }

    /**
     * 当接收到用户上传的消息
     *
     * @param userId
     * @param session
     */
    @OnMessage
    public void onMessage(@PathParam(value = "userId") String userId, Session session, String message) {
        String msg = "[" + userId + "]:" + message;
        System.out.println("接收到信息:" + msg);
        // 直接广播
        WebsocketUtil.sendMessageForAll(msg);
    }

    /**
     * 处理用户活连接异常
     *
     * @param session
     * @param throwable
     */
    @OnError
    public void onError(Session session, Throwable throwable) {
        try {
            session.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        throwable.printStackTrace();
    }
}

前端页面

1、前端普通js页面

userId根据业务需求自己定义,属于唯一标识

	const socket = new WebSocket('ws://127.0.0.1:8080/chat/userId');

    // 成功连接 WebSocket 服务器的回调函数
    socket.onopen = function() {
        console.log('WebSocket 连接成功');
        // 向服务器发送消息
        socket.send('Hello Server');
    };

    // 接收到来自 WebSocket 服务器的消息时的回调函数
    socket.onmessage = function(event) {
        console.info(event)
        const message = event.data;
        console.log('接收到服务器的消息:', message);
    };

    // WebSocket 连接关闭时的回调函数
    socket.onclose = function(event) {
        console.log('WebSocket 连接关闭');
    };

    // WebSocket 连接发生错误时的回调函数
    socket.onerror = function(error) {
        console.error('WebSocket 发生错误:', error);
    };

求关注~~~

点关注不迷路,喜欢的朋友们关注支持一下
给点继续写的动力,感谢!!
### 实现支付流程模拟 #### 添加 WebSocket 依赖 为了在 Spring Boot 中集成 WebSocket 功能,在 `pom.xml` 文件中需加入如下依赖[^1]: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> <version>${spring-boot.version}</version> </dependency> ``` #### 创建 WebSocket 配置类 创建一个用于配置 WebSocketJava 类,该类负责注册 WebSocket 终端处理器。具体代码如下所示[^2]: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter; @Configuration public class WebSocketConfig { @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } } ``` 此部分确保服务器能够处理来自客户端的连接请求。 #### 构建 WebSocket 处理器 定义一个新的类来管理具体的会话逻辑以及消息传递机制。这里假设名为 `PaymentSocketHandler.java`: ```java import java.io.IOException; import javax.websocket.OnClose; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @ServerEndpoint("/ws/payment") @Component public class PaymentSocketHandler { private static final Logger logger = LoggerFactory.getLogger(PaymentSocketHandler.class); @OnOpen public void onOpen(Session session) { logger.info("New connection with client: {}", session.getId()); } @OnMessage public String onMessage(String message, Session session) throws IOException { logger.info("Received payment request from {}: {}", session.getId(), message); // Simulate processing the payment and send back a response. boolean success = simulatePaymentProcessing(message); if (success) { return "Payment successful"; } else { throw new RuntimeException("Failed to process payment"); } } @OnClose public void onClose(Session session) { logger.info("Connection closed for client: {}", session.getId()); } private boolean simulatePaymentProcessing(String details){ try{ Thread.sleep(2000L); // Simulating delay during transaction return true; // Assuming all payments succeed here as this is just an example }catch(Exception e){ return false; } } } ``` 这段代码实现了基本的消息接收、发送和关闭事件监听,并提供了一个简单的支付过程仿真方法。 #### 前端 Vue.js 连接设置 对于前端应用来说,则需要利用 JavaScript 来建立与上述服务端点之间的通信链接。可以借助于标准 API 或者第三方库如 Socket.IO 完成这项工作。下面是一个基于原生 WebSocket 接口的例子: ```javascript // src/components/Payment.vue export default { data () { return { socket: null, status: 'Disconnected' }; }, mounted () { const wsUrl = `${process.env.VUE_APP_WS_URL}/ws/payment`; this.socket = new WebSocket(wsUrl); this.socket.onopen = () => { console.log('Connected'); this.status = 'Connected'; }; this.socket.onmessage = evt => { alert(`Response received : ${evt.data}`); }; this.socket.onerror = error => { console.error(`WebSocket Error: ${error.message}`); }; this.socket.onclose = event => { console.warn(`Client was disconnected (${event.code})`); this.status = 'Disconnected'; }; }, methods: { sendMessage () { let msg = JSON.stringify({ amount: 9.99 }); this.socket.send(msg); } }, beforeDestroy () { this.socket.close(); } }; ``` 以上就是整个实现方案的大致框架,实际开发过程中可能还需要考虑更多细节问题比如安全性验证等。
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

乾坤鸟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值