Skip to content

Commit ac773d9

Browse files
committed
Polishing contribution
Closes gh-34828
1 parent bd70072 commit ac773d9

File tree

1 file changed

+50
-59
lines changed

1 file changed

+50
-59
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/socket/WebSocketHandler.java

+50-59
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,78 +23,69 @@
2323
import reactor.core.publisher.Mono;
2424

2525
/**
26-
* Handler for a WebSocket session.
27-
*
28-
* <p>A server {@code WebSocketHandler} is mapped to requests with
26+
* Handler for a WebSocket messages. You can use it as follows:
27+
* <ul>
28+
* <li>On the server side, {@code WebSocketHandler} is mapped to requests with
2929
* {@link org.springframework.web.reactive.handler.SimpleUrlHandlerMapping
3030
* SimpleUrlHandlerMapping} and
3131
* {@link org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter
32-
* WebSocketHandlerAdapter}. A client {@code WebSocketHandler} is passed to the
32+
* WebSocketHandlerAdapter}.
33+
* <li>On the client side, {@code WebSocketHandler} is passed into the
3334
* {@link org.springframework.web.reactive.socket.client.WebSocketClient
3435
* WebSocketClient} execute method.
36+
* </ul>
3537
*
36-
* <p>Use {@link WebSocketSession#receive() session.receive()} to compose on
37-
* the inbound message stream, and {@link WebSocketSession#send(Publisher)
38-
* session.send(publisher)} for the outbound message stream. Below is an
39-
* example, combined flow to process inbound and to send outbound messages:
38+
* <p>{@link WebSocketSession#receive() session.receive()} handles inbound
39+
* messages, while {@link WebSocketSession#send(Publisher) session.send}
40+
* sends outbound messages. Below is an example of handling inbound messages
41+
* and responding to every message:
4042
*
4143
* <pre class="code">
42-
* class ExampleHandler implements WebSocketHandler {
43-
*
44-
* &#064;Override
45-
* public Mono&lt;Void&gt; handle(WebSocketSession session) {
46-
*
47-
* Flux&lt;WebSocketMessage&gt; output = session.receive()
48-
* .doOnNext(message -&gt; {
49-
* // This is for side effects such as
50-
* // - Logging incoming messages
51-
* // - Updating some metrics or counters
52-
* // - Performing access checks or validations (non-blocking)
53-
* System.out.println("Got message: " + message.getPayloadAsText());
54-
* })
55-
* .concatMap(message -&gt; {
56-
* // This is where you handle the actual processing of the incoming message. It
57-
* // might involve:
58-
* // - Parsing the message content (e.g., JSON parsing)
59-
* // - Invoking a reactive service (e.g., database, HTTP call, etc.)
60-
* // - Returning a transformed value, typically a Mono&lt;String&gt; or Mono&lt;SomeType&gt;
61-
* // if you're mapping to another data format
62-
* return Mono.just(message.getPayloadAsText());
63-
* })
64-
* .map(value -&gt; {
65-
* // This is where you produce one or more responses for the message
66-
* return session.textMessage("Echo " + value));
67-
* });
68-
*
69-
* return session.send(output);
70-
* }
71-
* }
44+
* class ExampleHandler implements WebSocketHandler {
45+
*
46+
* &#064;Override
47+
* public Mono&lt;Void&gt; handle(WebSocketSession session) {
48+
* Flux&lt;WebSocketMessage&gt; output = session.receive()
49+
* .doOnNext(message -&gt; {
50+
* // Imperative calls without a return value:
51+
* // perform access checks, log, validate, update metrics.
52+
* // ...
53+
* })
54+
* .concatMap(message -&gt; {
55+
* // Async, non-blocking calls:
56+
* // parse messages, call a database, make remote calls.
57+
* // Return the same message, or a transformed value
58+
* // ...
59+
* });
60+
* return session.send(output);
61+
* }
62+
* }
7263
* </pre>
7364
*
7465
* <p>If processing inbound and sending outbound messages are independent
7566
* streams, they can be joined together with the "zip" operator:
7667
*
7768
* <pre class="code">
78-
* class ExampleHandler implements WebSocketHandler {
79-
*
80-
* &#064;Override
81-
* public Mono&lt;Void&gt; handle(WebSocketSession session) {
82-
*
83-
* Mono&lt;Void&gt; input = session.receive()
84-
* .doOnNext(message -&gt; {
85-
* // ...
86-
* })
87-
* .concatMap(message -&gt; {
88-
* // ...
89-
* })
90-
* .then();
91-
*
92-
* Flux&lt;String&gt; source = ... ;
93-
* Mono&lt;Void&gt; output = session.send(source.map(session::textMessage));
94-
*
95-
* return Mono.zip(input, output).then();
96-
* }
97-
* }
69+
* class ExampleHandler implements WebSocketHandler {
70+
*
71+
* &#064;Override
72+
* public Mono&lt;Void&gt; handle(WebSocketSession session) {
73+
*
74+
* Mono&lt;Void&gt; input = session.receive()
75+
* .doOnNext(message -&gt; {
76+
* // ...
77+
* })
78+
* .concatMap(message -&gt; {
79+
* // ...
80+
* })
81+
* .then();
82+
*
83+
* Flux&lt;String&gt; source = ... ;
84+
* Mono&lt;Void&gt; output = session.send(source.map(session::textMessage));
85+
*
86+
* return Mono.zip(input, output).then();
87+
* }
88+
* }
9889
* </pre>
9990
*
10091
* <p>A {@code WebSocketHandler} must compose the inbound and outbound streams

0 commit comments

Comments
 (0)