liusuyi
2024-07-10 016aa105789fca71e86cdbb0d26a181812f200f9
ard-work/src/main/java/com/ruoyi/utils/websocket/service/ChatServerEndpoint.java
@@ -1,6 +1,8 @@
package com.ruoyi.utils.websocket.service;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.app.position.service.impl.AppPositionPushService;
import com.ruoyi.call.dto.CallMessage;
import com.ruoyi.utils.websocket.util.WebSocketUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@@ -9,6 +11,11 @@
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.ruoyi.utils.websocket.util.WebSocketUtils.ONLINE_USER_SESSIONS;
/**
 * @ClassName ChatServerEndpoint
@@ -33,11 +40,27 @@
    @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);
//        Session session = WebSocketUtils.ONLINE_USER_SESSIONS.get(userId);
//        WebSocketUtils.sendMessage(session, message);
        Map<String, Object> messageMap = JSONObject.parseObject(message, Map.class);
        if (((String) messageMap.get("type")).equals("callMessage")) {
            String msg = (String) messageMap.get("message");
            CallMessage callMessage = JSONObject.parseObject(msg, CallMessage.class);
            if (callMessage != null && callMessage.getType().equals(0)) {
                String regex = "^" + Pattern.quote(callMessage.getTargetId()) + "_\\d+$";
                Pattern pattern = Pattern.compile(regex);
                ONLINE_USER_SESSIONS.entrySet().stream()
                        .filter(entry -> {
                            Matcher matcher = pattern.matcher(entry.getKey());
                            return matcher.matches();
                        })
                        .map(Map.Entry::getValue)
                        .forEach(session -> WebSocketUtils.sendMessage(session, message));
            }
        }
        // 根据用户新的频率重新调整定时任务
        AppPositionPushService.messageHandler(userId,message);
        AppPositionPushService.messageHandler(userId, message);
    }
    @OnClose