feat:
- 添加WebSocket推送配置
This commit is contained in:
parent
86d70f8a45
commit
447f1d7162
6
pom.xml
6
pom.xml
@ -44,6 +44,12 @@
|
|||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring WebSocket-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- Spring Security -->
|
<!-- Spring Security -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
|||||||
@ -0,0 +1,33 @@
|
|||||||
|
package asia.yulinling.workflow.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
|
||||||
|
import org.springframework.web.socket.config.annotation.EnableWebSocket;
|
||||||
|
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
|
||||||
|
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
||||||
|
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* WebSocket配置
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author YLL
|
||||||
|
* @since 2025/6/22
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@EnableWebSocket
|
||||||
|
@EnableWebSocketMessageBroker
|
||||||
|
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
|
||||||
|
@Override
|
||||||
|
public void registerStompEndpoints(StompEndpointRegistry registry) {
|
||||||
|
// 注册一个 /notification 端点,前端通过这个端点进行连接
|
||||||
|
registry.addEndpoint("/websocket").setAllowedOrigins("*").withSockJS();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configureMessageBroker(MessageBrokerRegistry registry) {
|
||||||
|
// 定义了一个客户端订阅地址的前缀信息,也就是客户端接收服务端发送消息的前缀信息
|
||||||
|
registry.enableSimpleBroker("/topic");
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,14 @@
|
|||||||
|
package asia.yulinling.workflow.constant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* WebSocketConst
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author YLL
|
||||||
|
* @since 2025/6/22
|
||||||
|
*/
|
||||||
|
public interface WebSocketConst {
|
||||||
|
String WEBSOCKET_PATH = "/websocket";
|
||||||
|
String PUSH_SERVER = "/topic/push";
|
||||||
|
}
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
package asia.yulinling.workflow.task;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* WebSocketTask
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author YLL
|
||||||
|
* @since 2025/6/22
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class WebSocketTask {
|
||||||
|
private final SimpMessagingTemplate simpMessagingTemplate;
|
||||||
|
|
||||||
|
// @Scheduled(fixedRate = 1000)
|
||||||
|
public void sendMessage() throws Exception{
|
||||||
|
log.info("【推送消息】开始执行:{}", DateUtil.formatDateTime(new Date()));
|
||||||
|
simpMessagingTemplate.convertAndSend("test", "1");
|
||||||
|
log.info("【推送消息】执行结束:{}", DateUtil.formatDateTime(new Date()));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user