I followed that tutorial (specifically the part with the Browser WebSocket Client): http://www.baeldung.com/spring-5-reactive-websockets Everything works fine.
I would like to go a step further and having my handler behave according to a parameter sent from the client side. On connection the client is sending a message ("event-me-from-browser"):
var clientWebSocket = new WebSocket("ws://localhost:8080/event-emitter");
clientWebSocket.onopen = function() {
console.log("clientWebSocket.onopen", clientWebSocket);
console.log("clientWebSocket.readyState", "websocketstatus");
clientWebSocket.send("event-me-from-browser");
}
I tried to retrieve that message on server side (java):
@Override
public Mono<Void> handle(WebSocketSession webSocketSession) {
webSocketSession.receive().handle((pWebSocketMessage, pSynchronousSink) -> System.out.println(pWebSocketMessage.getPayloadAsText()));
return webSocketSession.send(Flux.fromStream(Stream.generate(() -> getData()))
.delayElements(Duration.ofMillis(50))
.map(webSocketSession::textMessage))
.and(webSocketSession.receive()
.map(WebSocketMessage::getPayloadAsText)
.log());
}
But it does not work.
Any idea? What did I do wrong?