|
|
|
@ -20,7 +20,7 @@ import java.util.function.Consumer;
|
|
|
|
|
import static org.dromara.common.websocket.constant.WebSocketConstants.WEB_SOCKET_TOPIC;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 工具类
|
|
|
|
|
* WebSocket工具类
|
|
|
|
|
*
|
|
|
|
|
* @author zendwang
|
|
|
|
|
*/
|
|
|
|
@ -29,10 +29,10 @@ import static org.dromara.common.websocket.constant.WebSocketConstants.WEB_SOCKE
|
|
|
|
|
public class WebSocketUtils {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送消息
|
|
|
|
|
* 向指定的WebSocket会话发送消息
|
|
|
|
|
*
|
|
|
|
|
* @param sessionKey session主键 一般为用户id
|
|
|
|
|
* @param message 消息文本
|
|
|
|
|
* @param sessionKey 要发送消息的用户id
|
|
|
|
|
* @param message 要发送的消息内容
|
|
|
|
|
*/
|
|
|
|
|
public static void sendMessage(Long sessionKey, String message) {
|
|
|
|
|
WebSocketSession session = WebSocketSessionHolder.getSessions(sessionKey);
|
|
|
|
@ -40,18 +40,18 @@ public class WebSocketUtils {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 订阅消息
|
|
|
|
|
* 订阅WebSocket消息主题,并提供一个消费者函数来处理接收到的消息
|
|
|
|
|
*
|
|
|
|
|
* @param consumer 自定义处理
|
|
|
|
|
* @param consumer 处理WebSocket消息的消费者函数
|
|
|
|
|
*/
|
|
|
|
|
public static void subscribeMessage(Consumer<WebSocketMessageDto> consumer) {
|
|
|
|
|
RedisUtils.subscribe(WEB_SOCKET_TOPIC, WebSocketMessageDto.class, consumer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发布订阅的消息
|
|
|
|
|
* 发布WebSocket订阅消息
|
|
|
|
|
*
|
|
|
|
|
* @param webSocketMessage 消息对象
|
|
|
|
|
* @param webSocketMessage 要发布的WebSocket消息对象
|
|
|
|
|
*/
|
|
|
|
|
public static void publishMessage(WebSocketMessageDto webSocketMessage) {
|
|
|
|
|
List<Long> unsentSessionKeys = new ArrayList<>();
|
|
|
|
@ -69,16 +69,16 @@ public class WebSocketUtils {
|
|
|
|
|
broadcastMessage.setMessage(webSocketMessage.getMessage());
|
|
|
|
|
broadcastMessage.setSessionKeys(unsentSessionKeys);
|
|
|
|
|
RedisUtils.publish(WEB_SOCKET_TOPIC, broadcastMessage, consumer -> {
|
|
|
|
|
log.info("WebSocket发送主题订阅消息topic:{} session keys:{} message:{}",
|
|
|
|
|
log.info(" WebSocket发送主题订阅消息topic:{} session keys:{} message:{}",
|
|
|
|
|
WEB_SOCKET_TOPIC, unsentSessionKeys, webSocketMessage.getMessage());
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发布订阅的消息(群发)
|
|
|
|
|
* 向所有的WebSocket会话发布订阅的消息(群发)
|
|
|
|
|
*
|
|
|
|
|
* @param message 消息内容
|
|
|
|
|
* @param message 要发布的消息内容
|
|
|
|
|
*/
|
|
|
|
|
public static void publishAll(String message) {
|
|
|
|
|
WebSocketMessageDto broadcastMessage = new WebSocketMessageDto();
|
|
|
|
@ -88,14 +88,31 @@ public class WebSocketUtils {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 向指定的WebSocket会话发送Pong消息
|
|
|
|
|
*
|
|
|
|
|
* @param session 要发送Pong消息的WebSocket会话
|
|
|
|
|
*/
|
|
|
|
|
public static void sendPongMessage(WebSocketSession session) {
|
|
|
|
|
sendMessage(session, new PongMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 向指定的WebSocket会话发送文本消息
|
|
|
|
|
*
|
|
|
|
|
* @param session WebSocket会话
|
|
|
|
|
* @param message 要发送的文本消息内容
|
|
|
|
|
*/
|
|
|
|
|
public static void sendMessage(WebSocketSession session, String message) {
|
|
|
|
|
sendMessage(session, new TextMessage(message));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 向指定的WebSocket会话发送WebSocket消息对象
|
|
|
|
|
*
|
|
|
|
|
* @param session WebSocket会话
|
|
|
|
|
* @param message 要发送的WebSocket消息对象
|
|
|
|
|
*/
|
|
|
|
|
private static void sendMessage(WebSocketSession session, WebSocketMessage<?> message) {
|
|
|
|
|
if (session == null || !session.isOpen()) {
|
|
|
|
|
log.warn("[send] session会话已经关闭");
|
|
|
|
|