feat():Socket
This commit is contained in:
@@ -12,6 +12,11 @@ public class RabbitConfig {
|
|||||||
return new Queue(CommonConstants.RabbitMQ.RECEIVE_MODEL, true);
|
return new Queue(CommonConstants.RabbitMQ.RECEIVE_MODEL, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Queue receiveRealTimeQueue() {
|
||||||
|
return new Queue(CommonConstants.RabbitMQ.RECEIVE_REAL_TIME, true);
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Queue sendModelQueue() {
|
public Queue sendModelQueue() {
|
||||||
return new Queue(CommonConstants.RabbitMQ.SEND_MODEL, true);
|
return new Queue(CommonConstants.RabbitMQ.SEND_MODEL, true);
|
||||||
|
|||||||
@@ -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("*"); //允许跨域
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,6 +24,8 @@ public class CommonConstants {
|
|||||||
|
|
||||||
public class RabbitMQ {
|
public class RabbitMQ {
|
||||||
public static final String RECEIVE_MODEL = "plateform.hmi.queue";
|
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";
|
public static final String SEND_MODEL = "plateform.modpt.queue";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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<String, Object> 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) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.fizz.business.mq.RabbitMQ;
|
package com.fizz.business.mq.RabbitMQ;
|
||||||
|
|
||||||
import cn.hutool.json.JSONUtil;
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.fizz.business.constants.CommonConstants;
|
||||||
import com.fizz.business.constants.enums.WsTypeEnum;
|
import com.fizz.business.constants.enums.WsTypeEnum;
|
||||||
import com.fizz.business.utils.WebSocketUtil;
|
import com.fizz.business.utils.WebSocketUtil;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
@@ -9,9 +10,17 @@ import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Log4j2
|
@Log4j2
|
||||||
//@Component
|
@Component
|
||||||
public class RabbitQueueListener {
|
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
|
@RabbitHandler
|
||||||
public void onProcDataMessage(String message) {
|
public void onProcDataMessage(String message) {
|
||||||
log.info("消费端ProcData: " + message);
|
log.info("消费端ProcData: " + message);
|
||||||
|
|||||||
@@ -115,6 +115,8 @@ public class SecurityConfig
|
|||||||
// 静态资源,可匿名访问
|
// 静态资源,可匿名访问
|
||||||
.antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
|
.antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
|
||||||
.antMatchers("/doc.html","/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
|
.antMatchers("/doc.html","/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
|
||||||
|
//websocket
|
||||||
|
.antMatchers("/websocket/**").permitAll()
|
||||||
// 除上面外的所有请求全部需要鉴权认证
|
// 除上面外的所有请求全部需要鉴权认证
|
||||||
.anyRequest().authenticated();
|
.anyRequest().authenticated();
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user