websocket是一个全双工协议,只需要建立一次长连接,前端可以不断向服务器发送请求,他的好处是减少了通信量和资源的浪费,服务器可以主动向前端推送数据
在该项目中,使用了定时器实现服务端生产消息并发送给前端,还有可以根据情况生产消息发送
springboot集成webSocket步骤:
1,导入依赖
org.springframework.boot
spring-boot-starter-websocket
2,websocketConfig配置类
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
p