Top Description Methods
org.springframework.web.reactive.socket

public Interface WebSocketHandler

Known Direct Implementers
org.springframework.web.reactive.socket.adapter.ContextWebSocketHandler
Imports
java.util.Collections, .List, org.reactivestreams.Publisher, reactor.core.publisher.Mono

Handler for a WebSocket session.

A server WebSocketHandler is mapped to requests with SimpleUrlHandlerMapping and WebSocketHandlerAdapter. A client WebSocketHandler is passed to the WebSocketClient execute method.

Use session.receive() to compose on the inbound message stream, and session.send(publisher) for the outbound message stream. Below is an example, combined flow to process inbound and to send outbound messages:

 class ExampleHandler implements WebSocketHandler {

 	@Override
 	public Mono<Void> handle(WebSocketSession session) {

 		Flux<WebSocketMessage> output = session.receive()
			.doOnNext(message -> {
 				// ...
 			})
 			.concatMap(message -> {
 				// ...
 			})
 			.map(value -> session.textMessage("Echo " + value));

 		return session.send(output);
 	}
 }
 

If processing inbound and sending outbound messages are independent streams, they can be joined together with the "zip" operator:

 class ExampleHandler implements WebSocketHandler {

 	@Override
 	public Mono<Void> handle(WebSocketSession session) {

 		Mono<Void> input = session.receive()
			.doOnNext(message -> {
 				// ...
 			})
 			.concatMap(message -> {
 				// ...
 			})
 			.then();

		Flux<String> source = ... ;
 		Mono<Void> output = session.send(source.map(session::textMessage));

 		return Mono.zip(input, output).then();
 	}
 }
 

A WebSocketHandler must compose the inbound and outbound streams into a unified flow and return a Mono<Void> that reflects the completion of that flow. That means there is no need to check if the connection is open, since Reactive Streams signals will terminate activity. The inbound stream receives a completion/error signal, and the outbound stream receives a cancellation signal.

Author
Rossen Stoyanchev
Since
5.0

Method Summary

Modifier and TypeMethod and Description
public default List<String>
getSubProtocols()

Return the list of sub-protocols supported by this handler.

public Mono<Void>

Returns:

indicates when application handling of the session is complete, which should reflect the completion of the inbound message stream (i.e. connection closing) and possibly the completion of the outbound message stream and the writing of messages
handle
(WebSocketSession
the session to handle
session
)

Invoked when a new WebSocket connection is established, and allows handling of the session.

Method Detail

getSubProtocolsback to summary
public default List<String> getSubProtocols()

Return the list of sub-protocols supported by this handler.

By default an empty list is returned.

handleback to summary
public Mono<Void> handle(WebSocketSession session)

Invoked when a new WebSocket connection is established, and allows handling of the session.

See the class-level doc and the reference manual for more details and examples of how to handle the session.

Parameters
session:WebSocketSession

the session to handle

Returns:Mono<Void>

indicates when application handling of the session is complete, which should reflect the completion of the inbound message stream (i.e. connection closing) and possibly the completion of the outbound message stream and the writing of messages