我一直在尝试从WebSocketSessionHandler(namespace:org.springframework.web.reactive.socket)中检索WebSession,以便在我的发送消费者中使用它(使用FluxSink)。
我一直在挖掘整个春天的源代码和文档,这些代码和文档似乎没有更新,我没有找到关于最新版本的任何内容。
是否可以检索WebSession或者是否有办法只获取id?
以下是示例代码: WebSocketHandler.java
public class WebSocketHandler implements org.springframework.web.reactive.socket.WebSocketHandler {
/**
* {@inheritDoc}
*/
@Override
public Mono<Void> handle(final WebSocketSession session) {
// use session to receive message etc...
return (session.send(Flux.create((FluxSink<OutputMessageInterface> sink) -> {
// use sink
}).map(/* ... convert from string to WebSocketMessage ... */)));
}
}
配置文件: WebSocketConfiguration.java
@Configuration
public class WebSocketConfiguration {
@Bean
public WebSocketHandler webSocketHandler() {
return (new WebSocketHandler());
}
@Bean
public WebSocketHandlerAdapter handlerAdapter() {
return (new WebSocketHandlerAdapter());
}
@Bean
public HandlerMapping handlerMapping(@Autowired WebSocketHandler webSocketHandler) {
Map<String, WebSocketHandler> map = new HashMap<>();
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
map.put("/realtime", webSocketHandler);
mapping.setOrder(10);
mapping.setUrlMap(map);
return (mapping);
}
}
该项目基于spring-boot 2.0.0.M6,spring-boot-starter-webflux 2.0.0.M6(spring-core 5.0.1.RELEASE)