Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

java - How to make Spring5 WebFlux works with WebSocket?

I'm actually trying to make a WebSocket server working but without success.

I'm using Spring 5 WebFlux and starting a Netty server with the following endpoint : https://localhost:443/ws/events

Here are my conf. files :

CORS handling :

@Configuration
@EnableWebFlux
public class CorsGlobalConfiguration implements WebFluxConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry corsRegistry) {
        corsRegistry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .maxAge(3600);
    }
}

WebSocket conf. :

@Configuration
@EnableWebFlux
public class WebSocketConfiguration {

    private final EventManager eventManager;

    public WebSocketConfiguration(EventManager eventManager) {
        this.eventManager = eventManager;
    }

    @Bean
    public HandlerMapping handlerMapping() {
        SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
        mapping.setUrlMap(Collections.singletonMap("/ws/events", new EventsWebSocketHandler(eventManager)));
        mapping.setOrder(-1);
        return mapping;
    }

    @Bean
    public HandlerAdapter handlerAdapter() {
        return new WebSocketHandlerAdapter();
    }
}

EventWebSocketHandler class :

@Component
public class EventsWebSocketHandler implements WebSocketHandler {

    private final EventManager eventManager;
    private ConcurrentHashMap<Request, Set<Event>> eventsMap;

    public EventsWebSocketHandler(EventManager eventManager) {
        this.eventManager = eventManager;
    }

    @Override
    public Mono<Void> handle(WebSocketSession webSocketSession) {
        this.eventsMap = eventManager.getMap();
        Flux<WebSocketMessage> output = webSocketSession
                .receive()
                .doOnNext(msg -> { convertToDTO(); });
        return webSocketSession.send(output);
    }

With Postman I've tried those settings (and many others) :

Headers

But I've got this error message :

Error message

I must say that the Sec-WebSocket-Key header has a value I've found on a web page related to this kind of problems. I really don't know where I could find a valid value for that and I'm far to be an expert about all of these knowledges related to servers, http protocol or SSL. At that point, I can not tell that the problem comes from that value.

Can someone share her/his own experience about WebFlux and WebSocket (Spring 5) and how to set this up so that it works ?

Thanx by advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...