zhubaomin
2025-04-07 e67870fff62635cd14beb0d5988f08aeef4b22fa
pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/largeScreen/WebSocketServer.java
@@ -1,6 +1,7 @@
package com.dy.pipIrrRemote.largeScreen;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import jakarta.websocket.*;
@@ -13,10 +14,13 @@
 * @Author: liurunyu
 * @Date: 2025/2/10 15:25
 * @Description 客户端(浏览器)每建立一个websocket连接,服务端就会创建一个WebSocketServer实例
 * 应用前提是存在一个Config类,如本模块的WebSocketConfig,这时@ServerEndpoint受SpringBoot容器
 * 管理了(在嵌入式web Servlet环境中)
 */
@Slf4j
@ServerEndpoint("/websocket/ls/{id}")
@Component
@Scope("prototype") // 非单例,每次请求都会创建新的实例
public class WebSocketServer {
    // 静态变量,记录当前在线连接数
@@ -27,7 +31,8 @@
    // 客户端连接会话,通过它给客户端发送数据
    private Session session;
    // 客户端上线时刻
    //private String onLineDt ;
    // 客户端id
    private String id = "";
@@ -39,7 +44,11 @@
    @OnOpen
    public void onOpen(Session session, @PathParam("id") String id) {
        this.session = session;
        //this.onLineDt = DateTime.yyyy_MM_dd_HH_mm_ss() ;
        this.id = id;
        if(this.id == null || this.id.length() == 0){
            this.id = "" + System.nanoTime() ;
        }
        this.sendMessage(WebSocketHeartBeat.getHeartBeatMessage());
        if (webSocketMap.containsKey(id)) {
@@ -98,7 +107,7 @@
    /**
     * 服务器主动群推送消息
     */
    public static void sendAllMessage(String message) throws IOException {
    public static void sendMessage2AllClient(String message) throws IOException {
        ConcurrentHashMap.KeySetView<String, WebSocketServer> ids = webSocketMap.keySet();
        for (String id : ids) {
            WebSocketServer webSocketServer = webSocketMap.get(id);
@@ -109,7 +118,7 @@
    /**
     * 服务器指定客户端推送消息
     */
    public static void sendOneMessage(String message, String id) throws IOException {
    public static void sendMessage2OneClient(String message, String id) throws IOException {
        if (message != null && message.length() != 0 && webSocketMap.containsKey(id)) {
            webSocketMap.get(id).sendMessage(message);
        } else {