- 添加WebSocket推送配置
This commit is contained in:
yulinling 2025-06-22 21:08:43 +08:00
parent 86d70f8a45
commit 447f1d7162
4 changed files with 84 additions and 0 deletions

View File

@ -44,6 +44,12 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring WebSocket-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -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");
}
}

View File

@ -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";
}

View File

@ -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()));
}
}