clll
2023-09-06 ee84c191faa2c235703eaa8cdc765729bed71a4f
springboot-vue3/src/main/java/com/example/springboot/component/WebSocketServer.java
@@ -4,8 +4,11 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.catalina.core.ApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.websocket.OnClose;
import javax.websocket.OnError;
@@ -25,116 +28,118 @@
@Component
public class WebSocketServer {
  static ConfigurableApplicationContext applicationContext;
  // 解决无法注入mapper问题  //使用方法
  // homeMapper=WebSocketServer.applicationContext.getBean(HomeMapper.class);
    // @Autowired
    // HomeMapper homeMapper;
  public static void setApplicationContext(ConfigurableApplicationContext configurableApplicationContext) {
    WebSocketServer.applicationContext = configurableApplicationContext;
  }
  private static final Logger log = LoggerFactory.getLogger(WebSocketServer.class);
  private List<String> messages;
  /**
   * 记录当前在线连接数
   */
  public static final Map<String, WebSocketServer> sessionMap = new ConcurrentHashMap<>();
  String username;
  Session session;
  public WebSocketServer() {
    this.messages = new ArrayList<>();
  }
  /**
   * 连接建立成功调用的方法
   */
  @OnOpen
  public void onOpen(Session session, @PathParam("username") String username) {
    this.username = username;
    this.session = session;
    sessionMap.put(username, this);
    log.info("有新用户加入,username={}, 当前在线人数为:{}", username, sessionMap.size());
    // JSONObject result = new JSONObject();
    // JSONArray array = new JSONArray();
    // result.set("users", array);
    // for (Object key : sessionMap.keySet()) {
    // JSONObject jsonObject = new JSONObject();
    // jsonObject.set("username", key);
    // array.add(jsonObject);
    // }
    // sendAllMessage(JSONUtil.toJsonStr(result)); // 后台发送消息给所有的客户端
  }
  /**
   * 连接关闭调用的方法
   */
  @OnClose
  public void onClose(Session session, @PathParam("username") String username) {
    sessionMap.remove(username);
    log.info("有一连接关闭,移除username={}的用户session, 当前在线人数为:{}", username, sessionMap.size());
  }
  /**
   * 收到客户端消息后调用的方法
   * 后台收到客户端发送过来的消息
   * onMessage 是一个消息的中转站
   * 接受 浏览器端 socket.send 发送过来的 json数据
   *
   * @param message 客户端发送过来的消息
   */
  @OnMessage
  public void onMessage(String message, Session session, @PathParam("username") String username) {
    log.info("服务端收到用户username={}的消息:{}", username, message);
    JSONObject obj = JSONUtil.parseObj(message);
    String text = obj.getStr("data");
    JSONObject jsonObject = new JSONObject();
    jsonObject.set("message", text);
    this.messages.add(text);
    this.sendMessage(jsonObject.toString()); // JSONUtil.toJsonStr(jsonObject)
  }
  @OnError
  public void onError(Session session, Throwable error) {
    log.error("发生错误");
    error.printStackTrace();
  }
  /**
   * 服务端发送消息给客户端
   */
  public void sendMessage(String message) {
    try {
      log.info("服务端给客户端[{}]发送消息{}", this.session.getId(), message);
      this.session.getBasicRemote().sendText(message);
    } catch (Exception e) {
      log.error("服务端发送消息给客户端失败", e);
    static ConfigurableApplicationContext applicationContext;
    //解决无法注入mapper问题  //使用方法 homeMapper=WebSocketServer.applicationContext.getBean(HomeMapper.class);
    public static void setApplicationContext(ConfigurableApplicationContext configurableApplicationContext) {
        WebSocketServer.applicationContext = configurableApplicationContext;
    }
  }
  /**
   * 服务端发送消息给所有客户端
   */
  public void sendAllMessage(String message) {
    try {
      for (WebSocketServer webSocketServer : sessionMap.values()) {
        log.info("服务端给客户端[{}]发送消息{}", this.session.getId(), message);
        webSocketServer.sendMessage(message);
      }
    } catch (Exception e) {
      log.error("服务端发送消息给客户端失败", e);
   private static final Logger log = LoggerFactory.getLogger(WebSocketServer.class);
    private List<String> messages;
    /**
     * 记录当前在线连接数
     */
    public static final Map<String, WebSocketServer> sessionMap = new ConcurrentHashMap<>();
    String username;
    Session session;
    public WebSocketServer() {
        this.messages = new ArrayList<>();
    }
  }
    /**
     * 连接建立成功调用的方法
     */
    @OnOpen
    public void onOpen(Session session, @PathParam("username") String username) {
        this.username=username;
        this.session=session;
        sessionMap.put(username, this);
        log.info("有新用户加入,username={}, 当前在线人数为:{}", username, sessionMap.size());
        // JSONObject result = new JSONObject();
        // JSONArray array = new JSONArray();
        // result.set("users", array);
        // for (Object key : sessionMap.keySet()) {
        //     JSONObject jsonObject = new JSONObject();
        //     jsonObject.set("username", key);
        //     array.add(jsonObject);
        // }
        //sendAllMessage(JSONUtil.toJsonStr(result));  // 后台发送消息给所有的客户端
    }
  public List<String> getMessages() {
    return messages;
  }
    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose(Session session, @PathParam("username") String username) {
        sessionMap.remove(username);
        log.info("有一连接关闭,移除username={}的用户session, 当前在线人数为:{}", username, sessionMap.size());
    }
  public void clearMessages() {
    messages.clear();
  }
    /**
     * 收到客户端消息后调用的方法
     * 后台收到客户端发送过来的消息
     * onMessage 是一个消息的中转站
     * 接受 浏览器端 socket.send 发送过来的 json数据
     * @param message 客户端发送过来的消息
     */
    @OnMessage
    public void onMessage(String message, Session session, @PathParam("username") String username) {
        log.info("服务端收到用户username={}的消息:{}", username, message);
        JSONObject obj = JSONUtil.parseObj(message);
         String text = obj.getStr("data");
         JSONObject jsonObject = new JSONObject();
         jsonObject.set("message", text);
        this.messages.add(text);
         this.sendMessage(jsonObject.toString()); //JSONUtil.toJsonStr(jsonObject)
    }
    @OnError
    public void onError(Session session, Throwable error) {
        log.error("发生错误");
        error.printStackTrace();
    }
    /**
     * 服务端发送消息给客户端
     */
    public void sendMessage(String message) {
        try {
            log.info("服务端给客户端[{}]发送消息{}", this.session.getId(), message);
            this.session.getBasicRemote().sendText(message);
        } catch (Exception e) {
            log.error("服务端发送消息给客户端失败", e);
        }
    }
    /**
     * 服务端发送消息给所有客户端
     */
    public void sendAllMessage(String message) {
        try {
            for (WebSocketServer webSocketServer : sessionMap.values()) {
                log.info("服务端给客户端[{}]发送消息{}", this.session.getId(), message);
                webSocketServer.sendMessage(message);
            }
        } catch (Exception e) {
            log.error("服务端发送消息给客户端失败", e);
        }
    }
    public List<String> getMessages() {
        return messages;
    }
    public void clearMessages() {
        messages.clear();
    }
}