From d2c776662449dcc07d8a7881d1c92ae592d752b9 Mon Sep 17 00:00:00 2001 From: Penknife Date: Fri, 10 Jan 2025 08:59:54 +0800 Subject: [PATCH] feat():Socket --- .../fizz/business/config/RabbitConfig.java | 5 +++ .../fizz/business/config/WebSocketConfig.java | 26 ++++++++++++ .../business/constants/CommonConstants.java | 2 + .../interceptor/TrackWsInterceptor.java | 40 +++++++++++++++++++ .../mq/RabbitMQ/RabbitQueueListener.java | 13 +++++- .../framework/config/SecurityConfig.java | 2 + 6 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 business/src/main/java/com/fizz/business/config/WebSocketConfig.java create mode 100644 business/src/main/java/com/fizz/business/interceptor/TrackWsInterceptor.java diff --git a/business/src/main/java/com/fizz/business/config/RabbitConfig.java b/business/src/main/java/com/fizz/business/config/RabbitConfig.java index 5d14e86..04bb97a 100644 --- a/business/src/main/java/com/fizz/business/config/RabbitConfig.java +++ b/business/src/main/java/com/fizz/business/config/RabbitConfig.java @@ -12,6 +12,11 @@ public class RabbitConfig { return new Queue(CommonConstants.RabbitMQ.RECEIVE_MODEL, true); } + @Bean + public Queue receiveRealTimeQueue() { + return new Queue(CommonConstants.RabbitMQ.RECEIVE_REAL_TIME, true); + } + @Bean public Queue sendModelQueue() { return new Queue(CommonConstants.RabbitMQ.SEND_MODEL, true); diff --git a/business/src/main/java/com/fizz/business/config/WebSocketConfig.java b/business/src/main/java/com/fizz/business/config/WebSocketConfig.java new file mode 100644 index 0000000..87c47c2 --- /dev/null +++ b/business/src/main/java/com/fizz/business/config/WebSocketConfig.java @@ -0,0 +1,26 @@ +package com.fizz.business.config; + +import com.fizz.business.interceptor.TrackWsInterceptor; +import com.fizz.business.service.client.TrackWsHandler; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.socket.config.annotation.EnableWebSocket; +import org.springframework.web.socket.config.annotation.WebSocketConfigurer; +import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; + +@Configuration +@EnableWebSocket +public class WebSocketConfig implements WebSocketConfigurer { + + @Autowired + TrackWsHandler trackWsHandler; + @Autowired + TrackWsInterceptor trackWsInterceptor; + + @Override + public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { + registry.addHandler(trackWsHandler, "/websocket") + .addInterceptors(trackWsInterceptor) + .setAllowedOrigins("*"); //允许跨域 + } +} diff --git a/business/src/main/java/com/fizz/business/constants/CommonConstants.java b/business/src/main/java/com/fizz/business/constants/CommonConstants.java index 6f79e8e..333109e 100644 --- a/business/src/main/java/com/fizz/business/constants/CommonConstants.java +++ b/business/src/main/java/com/fizz/business/constants/CommonConstants.java @@ -24,6 +24,8 @@ public class CommonConstants { public class RabbitMQ { public static final String RECEIVE_MODEL = "plateform.hmi.queue"; + public static final String RECEIVE_REAL_TIME = "plateform.realtime.queue"; + public static final String SEND_MODEL = "plateform.modpt.queue"; } diff --git a/business/src/main/java/com/fizz/business/interceptor/TrackWsInterceptor.java b/business/src/main/java/com/fizz/business/interceptor/TrackWsInterceptor.java new file mode 100644 index 0000000..95ad92b --- /dev/null +++ b/business/src/main/java/com/fizz/business/interceptor/TrackWsInterceptor.java @@ -0,0 +1,40 @@ +package com.fizz.business.interceptor; + +import com.fizz.business.constants.enums.WsTypeEnum; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.server.ServerHttpRequest; +import org.springframework.http.server.ServerHttpResponse; +import org.springframework.http.server.ServletServerHttpRequest; +import org.springframework.stereotype.Component; +import org.springframework.web.socket.WebSocketHandler; +import org.springframework.web.socket.server.HandshakeInterceptor; + +import java.util.Map; +import java.util.Objects; + +@Slf4j +@Component +public class TrackWsInterceptor implements HandshakeInterceptor { + + @Override + public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map attributes) throws Exception { + try { + String type = ((ServletServerHttpRequest) request).getServletRequest().getParameter("type"); + if (Objects.isNull(WsTypeEnum.getByValue(type))) { + log.error("[websocket]beforeHandshake error: type={}", type); + return false; + } + attributes.put("type", type); + return true; + } catch (Exception e) { + log.error("[websocket]beforeHandshake error", e); + return false; + } + + } + + @Override + public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) { + + } +} \ No newline at end of file diff --git a/business/src/main/java/com/fizz/business/mq/RabbitMQ/RabbitQueueListener.java b/business/src/main/java/com/fizz/business/mq/RabbitMQ/RabbitQueueListener.java index 9e20272..5b4e7da 100644 --- a/business/src/main/java/com/fizz/business/mq/RabbitMQ/RabbitQueueListener.java +++ b/business/src/main/java/com/fizz/business/mq/RabbitMQ/RabbitQueueListener.java @@ -1,6 +1,7 @@ package com.fizz.business.mq.RabbitMQ; import cn.hutool.json.JSONUtil; +import com.fizz.business.constants.CommonConstants; import com.fizz.business.constants.enums.WsTypeEnum; import com.fizz.business.utils.WebSocketUtil; import lombok.extern.log4j.Log4j2; @@ -9,9 +10,17 @@ import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Log4j2 -//@Component +@Component public class RabbitQueueListener { - @RabbitListener(queues = "plateform.hmi.queue") + @RabbitListener(queues = CommonConstants.RabbitMQ.RECEIVE_MODEL) + @RabbitHandler + public void onHmiMessage(String message) { + log.info("消费端ProcData: " + message); + + //socket + } + + @RabbitListener(queues = CommonConstants.RabbitMQ.RECEIVE_REAL_TIME) @RabbitHandler public void onProcDataMessage(String message) { log.info("消费端ProcData: " + message); diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java index 42d9646..8240ee3 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java @@ -115,6 +115,8 @@ public class SecurityConfig // 静态资源,可匿名访问 .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll() .antMatchers("/doc.html","/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll() + //websocket + .antMatchers("/websocket/**").permitAll() // 除上面外的所有请求全部需要鉴权认证 .anyRequest().authenticated(); })