feat: websocket config endpoints

This commit is contained in:
Zhongheng Liu 2025-11-27 14:51:38 +01:00
commit dcb76612d5
Signed by: steven
GPG key ID: F69B980899C1C09D

View file

@ -0,0 +1,33 @@
package server;
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
public void configureMessageBroker(MessageBrokerRegistry registry) {
// SUBSCRIBE /subscribe/{mapping}, see UpdateMessagingController definition
// adds a broker endpoint subscribers listen to
registry.enableSimpleBroker("/subscribe");
// SEND /send/updates/{mapping}, see UpdateMessagingController definition
// the client pushes a new message to this address
registry.setApplicationDestinationPrefixes("/send");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
// a client can now push data to /send/updates/{mapping}, see UpdateMessagingController
// allow access from any origin: one server, many clients
registry.addEndpoint("/updates").setAllowedOrigins("*");
// optional, recommended to increase browser & networking support
registry.addEndpoint("/updates").setAllowedOrigins("*").withSockJS();
}
}