liusuyi
2024-07-30 5f07ddc52c8643695049c0395aa73efc913ca69d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.ruoyi.call.listener;
 
import com.alibaba.fastjson2.JSON;
import com.ruoyi.call.domain.ArdCallHistory;
import com.ruoyi.call.dto.MessageEvent;
import com.ruoyi.utils.websocket.util.WebSocketUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
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;
 
/**
 * @Description: 事件监听
 * @ClassName: MsgListener
 * @Author: 刘苏义
 * @Date: 2023年09月23日8:37:00
 **/
@Component
@Slf4j(topic = "msgListener")
public class MsgListener {
 
    @Async
    @EventListener(MessageEvent.class)
    public void ArdCallHistoryEventListener(MessageEvent messageEvent) {
        log.debug("监听到会话消息事件:"+messageEvent.getArdCallHistory().getContent());
        log.debug("在线用户数量:"+ONLINE_USER_SESSIONS.size());
        String targetId=messageEvent.getTargetId();
        ArdCallHistory ardCallHistory = messageEvent.getArdCallHistory();
        // 构建正则表达式模式
        String regex = "^" + Pattern.quote(targetId) + "_\\d+$";
        Pattern pattern = Pattern.compile(regex);
        Map<String, Object> messageMap = new HashMap<>();
        messageMap.put("type", "message");
        messageMap.put("message", JSON.toJSONString(ardCallHistory));
        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, messageMap));
    }
}