liusuyi
2024-10-10 38f29e38fcc668171dc05c53d40a36b895c86102
ard-work/src/main/java/com/ruoyi/utils/websocket/service/ChatServerEndpoint.java
@@ -1,6 +1,13 @@
package com.ruoyi.utils.websocket.service;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.app.position.service.impl.AppPositionPushService;
import com.ruoyi.call.dto.CallMessage;
import com.ruoyi.call.listener.WebsocketHandler;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.utils.websocket.util.WebSocketUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@@ -9,6 +16,14 @@
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.ruoyi.utils.websocket.util.WebSocketUtils.ONLINE_USER_SESSIONS;
/**
 * @ClassName ChatServerEndpoint
@@ -32,22 +47,30 @@
    @OnMessage
    public void onMessage(@PathParam("userId") String userId, String message) {
        log.info("收到消息:" + message);
        Session session = WebSocketUtils.ONLINE_USER_SESSIONS.get(userId);
        WebSocketUtils.sendMessage(session, message);
        //sendMessageAll("用户[" + userid + "] : " + message);
        // 根据用户新的频率重新调整定时任务
        AppPositionPushService.messageHandler(userId,message);
        log.debug("收到消息:" + message);
        try {
            if(isValidJson(message)) {
                // 处理视频通话消息
                WebsocketHandler.handleMessage(userId, message);
                // 根据用户新的频率重新调整定时任务
                AppPositionPushService.messageHandler(userId, message);
            } else {
                // 处理其他消息
                WebSocketUtils.sendMessage(userId,"Received an invalid JSON message:"+message);
            }
        } catch (Exception ex) {
            WebSocketUtils.sendMessage(userId, "无法解析消息【" + message + "】:" + ex.getMessage());
        }
    }
    @OnClose
    public void onClose(@PathParam("userId") String userId, Session session) {
        //当前的Session 移除
        WebSocketUtils.ONLINE_USER_SESSIONS.remove(userId);
        //并且通知其他人当前用户已经离开聊天室了
        String message = "用户[" + userId + "] 断开连接!";
        //sendMessage(session,message);
        log.info("消息:" + message);
        //从房间中移除用户
        WebSocketUtils.ROOM_USER_SET.values().stream().forEach(userSet -> userSet.remove(userId));
        try {
            session.close();
            AppPositionPushService.stopHandler(userId);
@@ -66,4 +89,19 @@
        log.info("Throwable msg " + throwable.getMessage());
    }
    /**
     * 检查给定的字符串是否为有效的JSON格式。
     *
     * @param message 要检查的字符串
     * @return 如果是有效的JSON字符串,返回true;否则返回false。
     */
    public static boolean isValidJson(String message) {
        try {
            Object json = JSON.parse(message);
            // 确保解析结果是JSONObject或JSONArray类型
            return (json instanceof JSONObject || json instanceof JSONArray);
        } catch (JSONException e) {
            return false;  // 解析失败,说明不是合法的JSON
        }
    }
}