Files
XH-XunKe/backend/src/main/java/com/xh/xunke/config/LiveSocketConfig.java
T

27 lines
1.0 KiB
Java

package com.xh.xunke.config;
import com.xh.xunke.live.LiveStatusSocket;
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 LiveSocketConfig implements WebSocketConfigurer {
private final LiveStatusSocket socket;
private final AppProperties props;
public LiveSocketConfig(LiveStatusSocket socket, AppProperties props) {
this.socket = socket;
this.props = props;
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
String[] origins = java.util.Arrays.stream(props.getCorsOrigins().split(","))
.map(String::trim).filter(item -> !item.isEmpty()).toArray(String[]::new);
registry.addHandler(socket, "/ws/events").setAllowedOrigins(origins);
}
}