提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.websocket.core.sender; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
|
4 |
import cn.hutool.core.util.StrUtil; |
|
5 |
import com.iailab.framework.common.util.json.JsonUtils; |
|
6 |
import com.iailab.framework.websocket.core.message.JsonWebSocketMessage; |
|
7 |
import com.iailab.framework.websocket.core.session.WebSocketSessionManager; |
|
8 |
import lombok.RequiredArgsConstructor; |
|
9 |
import lombok.extern.slf4j.Slf4j; |
|
10 |
import org.springframework.web.socket.TextMessage; |
|
11 |
import org.springframework.web.socket.WebSocketSession; |
|
12 |
|
|
13 |
import java.io.IOException; |
|
14 |
import java.util.Collection; |
|
15 |
import java.util.Collections; |
|
16 |
import java.util.List; |
|
17 |
|
|
18 |
/** |
|
19 |
* WebSocketMessageSender 实现类 |
|
20 |
* |
|
21 |
* @author iailab |
|
22 |
*/ |
|
23 |
@Slf4j |
|
24 |
@RequiredArgsConstructor |
|
25 |
public abstract class AbstractWebSocketMessageSender implements WebSocketMessageSender { |
|
26 |
|
|
27 |
private final WebSocketSessionManager sessionManager; |
|
28 |
|
|
29 |
@Override |
|
30 |
public void send(Integer userType, Long userId, String messageType, String messageContent) { |
|
31 |
send(null, userType, userId, messageType, messageContent); |
|
32 |
} |
|
33 |
|
|
34 |
@Override |
|
35 |
public void send(Integer userType, String messageType, String messageContent) { |
|
36 |
send(null, userType, null, messageType, messageContent); |
|
37 |
} |
|
38 |
|
|
39 |
@Override |
|
40 |
public void send(String sessionId, String messageType, String messageContent) { |
|
41 |
send(sessionId, null, null, messageType, messageContent); |
|
42 |
} |
|
43 |
|
|
44 |
/** |
|
45 |
* 发送消息 |
|
46 |
* |
|
47 |
* @param sessionId Session 编号 |
|
48 |
* @param userType 用户类型 |
|
49 |
* @param userId 用户编号 |
|
50 |
* @param messageType 消息类型 |
|
51 |
* @param messageContent 消息内容 |
|
52 |
*/ |
|
53 |
public void send(String sessionId, Integer userType, Long userId, String messageType, String messageContent) { |
|
54 |
// 1. 获得 Session 列表 |
|
55 |
List<WebSocketSession> sessions = Collections.emptyList(); |
|
56 |
if (StrUtil.isNotEmpty(sessionId)) { |
|
57 |
WebSocketSession session = sessionManager.getSession(sessionId); |
|
58 |
if (session != null) { |
|
59 |
sessions = Collections.singletonList(session); |
|
60 |
} |
|
61 |
} else if (userType != null && userId != null) { |
|
62 |
sessions = (List<WebSocketSession>) sessionManager.getSessionList(userType, userId); |
|
63 |
} else if (userType != null) { |
|
64 |
sessions = (List<WebSocketSession>) sessionManager.getSessionList(userType); |
|
65 |
} |
|
66 |
if (CollUtil.isEmpty(sessions)) { |
|
67 |
log.info("[send][sessionId({}) userType({}) userId({}) messageType({}) messageContent({}) 未匹配到会话]", |
|
68 |
sessionId, userType, userId, messageType, messageContent); |
|
69 |
} |
|
70 |
// 2. 执行发送 |
|
71 |
doSend(sessions, messageType, messageContent); |
|
72 |
} |
|
73 |
|
|
74 |
/** |
|
75 |
* 发送消息的具体实现 |
|
76 |
* |
|
77 |
* @param sessions Session 列表 |
|
78 |
* @param messageType 消息类型 |
|
79 |
* @param messageContent 消息内容 |
|
80 |
*/ |
|
81 |
public void doSend(Collection<WebSocketSession> sessions, String messageType, String messageContent) { |
|
82 |
JsonWebSocketMessage message = new JsonWebSocketMessage().setType(messageType).setContent(messageContent); |
|
83 |
String payload = JsonUtils.toJsonString(message); // 关键,使用 JSON 序列化 |
|
84 |
sessions.forEach(session -> { |
|
85 |
// 1. 各种校验,保证 Session 可以被发送 |
|
86 |
if (session == null) { |
|
87 |
log.error("[doSend][session 为空, message({})]", message); |
|
88 |
return; |
|
89 |
} |
|
90 |
if (!session.isOpen()) { |
|
91 |
log.error("[doSend][session({}) 已关闭, message({})]", session.getId(), message); |
|
92 |
return; |
|
93 |
} |
|
94 |
// 2. 执行发送 |
|
95 |
try { |
|
96 |
session.sendMessage(new TextMessage(payload)); |
|
97 |
log.info("[doSend][session({}) 发送消息成功,message({})]", session.getId(), message); |
|
98 |
} catch (IOException ex) { |
|
99 |
log.error("[doSend][session({}) 发送消息失败,message({})]", session.getId(), message, ex); |
|
100 |
} |
|
101 |
}); |
|
102 |
} |
|
103 |
|
|
104 |
} |