feat: websocket stomp 添加对 ws 连接协议支持(uni-app原生)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package com.youlai.system.config;
|
||||
|
||||
import com.youlai.system.websocket.interceptor.AuthChannelInterceptor;
|
||||
import com.youlai.system.interceptor.WebsocketChannelInterceptor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -22,18 +22,18 @@ import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerCo
|
||||
@RequiredArgsConstructor
|
||||
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
|
||||
|
||||
private final AuthChannelInterceptor authChannelInterceptor;
|
||||
private final WebsocketChannelInterceptor websocketChannelInterceptor;
|
||||
|
||||
/**
|
||||
* 注册一个端点,客户端通过这个端点进行连接
|
||||
* 注册一个端点,客户端通过这个端点进行连接
|
||||
*/
|
||||
@Override
|
||||
public void registerStompEndpoints(StompEndpointRegistry registry) {
|
||||
registry
|
||||
.addEndpoint("/ws") // 注册了一个 /ws 的端点
|
||||
.setAllowedOriginPatterns("*") // 允许跨域的 WebSocket 连接
|
||||
.withSockJS() // 启用 SockJS (浏览器不支持WebSocket,SockJS 将会提供兼容性支持)
|
||||
;
|
||||
.withSockJS(); // 启用 SockJS (浏览器不支持WebSocket,SockJS 将会提供兼容性支持)
|
||||
registry.addEndpoint("/ws-app").setAllowedOriginPatterns("*"); // 注册了一个 /ws-app 的端点,支持 uni-app 的 ws 连接协议
|
||||
}
|
||||
|
||||
|
||||
@@ -60,6 +60,6 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
|
||||
*/
|
||||
@Override
|
||||
public void configureClientInboundChannel(ChannelRegistration registration) {
|
||||
registration.interceptors(authChannelInterceptor);
|
||||
registration.interceptors(websocketChannelInterceptor);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.youlai.system.websocket;
|
||||
package com.youlai.system.controller;
|
||||
|
||||
import com.youlai.system.websocket.dto.MessageDTO;
|
||||
import com.youlai.system.model.dto.SocketMessage;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.messaging.handler.annotation.DestinationVariable;
|
||||
@@ -51,7 +51,7 @@ public class WebsocketController {
|
||||
//@SendToUser(value = "/queue/greeting")
|
||||
public void sendToUser(Principal principal, @DestinationVariable String username, String message) {
|
||||
log.info("sender:{};receiver:{}", principal.getName(), username);
|
||||
messagingTemplate.convertAndSendToUser(username, "/queue/greeting", new MessageDTO(principal.getName(), message));
|
||||
messagingTemplate.convertAndSendToUser(username, "/queue/greeting", new SocketMessage(principal.getName(), message));
|
||||
/// return "Hello, " + message;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.youlai.system.websocket.interceptor;
|
||||
package com.youlai.system.interceptor;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.youlai.system.security.jwt.JwtTokenProvider;
|
||||
@@ -21,7 +21,7 @@ import java.security.Principal;
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class AuthChannelInterceptor implements ChannelInterceptor {
|
||||
public class WebsocketChannelInterceptor implements ChannelInterceptor {
|
||||
|
||||
private final JwtTokenProvider jwtTokenProvider;
|
||||
|
||||
@@ -38,14 +38,10 @@ public class AuthChannelInterceptor implements ChannelInterceptor {
|
||||
assert accessor != null;
|
||||
|
||||
if (StompCommand.CONNECT.equals(accessor.getCommand())) {
|
||||
// get token from header
|
||||
String bearerToken = accessor.getFirstNativeHeader("Authorization");
|
||||
// if token is not null
|
||||
if (StrUtil.isNotBlank(bearerToken)) {
|
||||
|
||||
bearerToken = bearerToken.substring(7);
|
||||
bearerToken = bearerToken.substring(7); // remove "Bearer "
|
||||
String username = jwtTokenProvider.getUsername(bearerToken);
|
||||
// if the username is not null, assign it to the Principal.
|
||||
if (StrUtil.isNotBlank(username)) {
|
||||
Principal principal = () -> username;
|
||||
accessor.setUser(principal);
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.youlai.system.listener.websocket;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.socket.messaging.SessionConnectedEvent;
|
||||
import org.springframework.web.socket.messaging.SessionDisconnectEvent;
|
||||
import org.springframework.web.socket.messaging.SessionSubscribeEvent;
|
||||
import org.springframework.web.socket.messaging.SessionUnsubscribeEvent;
|
||||
|
||||
/**
|
||||
* Websocket 客户端事件监听器
|
||||
*
|
||||
* @author haoxr
|
||||
* @since 2023/10/10
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class WebSocketEventListener {
|
||||
|
||||
/**
|
||||
* 监听客户端连接事件
|
||||
*
|
||||
* @param event 连接事件对象
|
||||
*/
|
||||
@EventListener
|
||||
public void handleWebSocketConnectListener(SessionConnectedEvent event) {
|
||||
log.info("客户端连接成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 监听客户端断开连接事件
|
||||
*
|
||||
* @param event 断开连接事件对象
|
||||
*/
|
||||
@EventListener
|
||||
public void handleWebSocketDisconnectListener(SessionDisconnectEvent event) {
|
||||
log.info("客户端断开连接");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 监听客户端订阅事件
|
||||
*
|
||||
* @param event 订阅事件对象
|
||||
*/
|
||||
@EventListener
|
||||
public void handleSubscription(SessionSubscribeEvent event) {
|
||||
log.info("客户端订阅:{}", JSONUtil.toJsonStr(event.getMessage()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 监听客户端取消订阅事件
|
||||
*
|
||||
* @param event 取消订阅事件对象
|
||||
*/
|
||||
@EventListener
|
||||
public void handleUnSubscription(SessionUnsubscribeEvent event) {
|
||||
log.info("客户端取消订阅:{}", JSONUtil.toJsonStr(event.getMessage()));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user