update 完善websocket包注释

2.X
AprilWind 8 months ago
parent a27d3ed2b7
commit 51e44860f5

@ -27,17 +27,20 @@ public class WebSocketConfig {
@Bean
public WebSocketConfigurer webSocketConfigurer(HandshakeInterceptor handshakeInterceptor,
WebSocketHandler webSocketHandler,
WebSocketProperties webSocketProperties) {
WebSocketHandler webSocketHandler, WebSocketProperties webSocketProperties) {
// 如果WebSocket的路径为空则设置默认路径为 "/websocket"
if (StrUtil.isBlank(webSocketProperties.getPath())) {
webSocketProperties.setPath("/websocket");
}
// 如果允许跨域访问的地址为空,则设置为 "*",表示允许所有来源的跨域请求
if (StrUtil.isBlank(webSocketProperties.getAllowedOrigins())) {
webSocketProperties.setAllowedOrigins("*");
}
// 返回一个WebSocketConfigurer对象用于配置WebSocket
return registry -> registry
// 添加WebSocket处理程序和拦截器到指定路径设置允许的跨域来源
.addHandler(webSocketHandler, webSocketProperties.getPath())
.addInterceptors(handshakeInterceptor)
.setAllowedOrigins(webSocketProperties.getAllowedOrigins());

@ -6,6 +6,7 @@ package org.dromara.common.websocket.constant;
* @author zendwang
*/
public interface WebSocketConstants {
/**
* websocketSessionkey
*/

@ -31,33 +31,42 @@ public class PlusWebSocketHandler extends AbstractWebSocketHandler {
}
/**
*
*
*
* @param session
* @param message
* @throws Exception
* @param session WebSocket
* @param message
* @throws Exception
*/
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
// 从WebSocket会话中获取登录用户信息
LoginUser loginUser = (LoginUser) session.getAttributes().get(LOGIN_USER_KEY);
List<Long> userIds = List.of(loginUser.getUserId());
// 创建WebSocket消息DTO对象
WebSocketMessageDto webSocketMessageDto = new WebSocketMessageDto();
webSocketMessageDto.setSessionKeys(userIds);
webSocketMessageDto.setSessionKeys(List.of(loginUser.getUserId()));
webSocketMessageDto.setMessage(message.getPayload());
WebSocketUtils.publishMessage(webSocketMessageDto);
}
/**
*
*
* @param session WebSocket
* @param message
* @throws Exception
*/
@Override
protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
super.handleBinaryMessage(session, message);
}
/**
*
* Pong
*
* @param session
* @param message
* @throws Exception
* @param session WebSocket
* @param message Pong
* @throws Exception
*/
@Override
protected void handlePongMessage(WebSocketSession session, PongMessage message) throws Exception {
@ -65,11 +74,11 @@ public class PlusWebSocketHandler extends AbstractWebSocketHandler {
}
/**
*
* WebSocket
*
* @param session
* @param exception
* @throws Exception
* @param session WebSocket
* @param exception
* @throws Exception
*/
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
@ -77,10 +86,10 @@ public class PlusWebSocketHandler extends AbstractWebSocketHandler {
}
/**
*
* WebSocket
*
* @param session
* @param status
* @param session WebSocket
* @param status
*/
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
@ -90,9 +99,9 @@ public class PlusWebSocketHandler extends AbstractWebSocketHandler {
}
/**
*
*
*
* @return
* @return truefalse
*/
@Override
public boolean supportsPartialMessages() {

@ -18,24 +18,52 @@ public class WebSocketSessionHolder {
private static final Map<Long, WebSocketSession> USER_SESSION_MAP = new ConcurrentHashMap<>();
/**
* WebSocketMap
*
* @param sessionKey
* @param session WebSocket
*/
public static void addSession(Long sessionKey, WebSocketSession session) {
USER_SESSION_MAP.put(sessionKey, session);
}
/**
* MapWebSocket
*
* @param sessionKey
*/
public static void removeSession(Long sessionKey) {
if (USER_SESSION_MAP.containsKey(sessionKey)) {
USER_SESSION_MAP.remove(sessionKey);
}
}
/**
* MapWebSocket
*
* @param sessionKey
* @return WebSocketnull
*/
public static WebSocketSession getSessions(Long sessionKey) {
return USER_SESSION_MAP.get(sessionKey);
}
/**
* MapWebSocket
*
* @return WebSocket
*/
public static Set<Long> getSessionsAll() {
return USER_SESSION_MAP.keySet();
}
/**
* Map
*
* @param sessionKey
* @return truefalse
*/
public static Boolean existSession(Long sessionKey) {
return USER_SESSION_MAP.containsKey(sessionKey);
}

@ -21,13 +21,13 @@ import static org.dromara.common.websocket.constant.WebSocketConstants.LOGIN_USE
public class PlusWebSocketInterceptor implements HandshakeInterceptor {
/**
*
* WebSocket
*
* @param request request
* @param response response
* @param wsHandler wsHandler
* @param attributes attributes
* @return
* @param request WebSocket
* @param response WebSocket
* @param wsHandler WebSocket
* @param attributes WebSocket
* @return truefalse
*/
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) {
@ -37,15 +37,16 @@ public class PlusWebSocketInterceptor implements HandshakeInterceptor {
}
/**
*
* WebSocket
*
* @param request request
* @param response response
* @param wsHandler wsHandler
* @param exception
* @param request WebSocket
* @param response WebSocket
* @param wsHandler WebSocket
* @param exception
*/
@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
// 在这个方法中可以执行一些握手成功后的后续处理逻辑,比如记录日志或者其他操作
}
}

@ -1,9 +1,9 @@
package org.dromara.common.websocket.listener;
import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.websocket.holder.WebSocketSessionHolder;
import org.dromara.common.websocket.utils.WebSocketUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
@ -16,8 +16,15 @@ import org.springframework.core.Ordered;
@Slf4j
public class WebSocketTopicListener implements ApplicationRunner, Ordered {
/**
* Spring BootWebSocket
*
* @param args
* @throws Exception
*/
@Override
public void run(ApplicationArguments args) throws Exception {
// 订阅WebSocket消息
WebSocketUtils.subscribeMessage((message) -> {
log.info("WebSocket主题订阅收到消息session keys={} message={}", message.getSessionKeys(), message.getMessage());
// 如果key不为空就按照key发消息 如果为空就群发

@ -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 {
});
}
/**
* WebSocketPong
*
* @param session PongWebSocket
*/
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));
}
/**
* WebSocketWebSocket
*
* @param session WebSocket
* @param message WebSocket
*/
private static void sendMessage(WebSocketSession session, WebSocketMessage<?> message) {
if (session == null || !session.isOpen()) {
log.warn("[send] session会话已经关闭");

Loading…
Cancel
Save