|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2021 the original author or authors. |
| 2 | + * Copyright 2002-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
23 | 23 | import reactor.core.publisher.Mono;
|
24 | 24 |
|
25 | 25 | /**
|
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 |
29 | 29 | * {@link org.springframework.web.reactive.handler.SimpleUrlHandlerMapping
|
30 | 30 | * SimpleUrlHandlerMapping} and
|
31 | 31 | * {@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 |
33 | 34 | * {@link org.springframework.web.reactive.socket.client.WebSocketClient
|
34 | 35 | * WebSocketClient} execute method.
|
| 36 | + * </ul> |
35 | 37 | *
|
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: |
40 | 42 | *
|
41 | 43 | * <pre class="code">
|
42 |
| - * class ExampleHandler implements WebSocketHandler { |
43 |
| - * |
44 |
| - * @Override |
45 |
| - * public Mono<Void> handle(WebSocketSession session) { |
46 |
| - * |
47 |
| - * Flux<WebSocketMessage> output = session.receive() |
48 |
| - * .doOnNext(message -> { |
49 |
| - * // ... |
50 |
| - * }) |
51 |
| - * .concatMap(message -> { |
52 |
| - * // ... |
53 |
| - * }) |
54 |
| - * .map(value -> session.textMessage("Echo " + value)); |
55 |
| - * |
56 |
| - * return session.send(output); |
57 |
| - * } |
58 |
| - * } |
| 44 | + * class ExampleHandler implements WebSocketHandler { |
| 45 | + * |
| 46 | + * @Override |
| 47 | + * public Mono<Void> handle(WebSocketSession session) { |
| 48 | + * Flux<WebSocketMessage> output = session.receive() |
| 49 | + * .doOnNext(message -> { |
| 50 | + * // Imperative calls without a return value: |
| 51 | + * // perform access checks, log, validate, update metrics. |
| 52 | + * // ... |
| 53 | + * }) |
| 54 | + * .concatMap(message -> { |
| 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 | + * } |
59 | 63 | * </pre>
|
60 | 64 | *
|
61 | 65 | * <p>If processing inbound and sending outbound messages are independent
|
62 | 66 | * streams, they can be joined together with the "zip" operator:
|
63 | 67 | *
|
64 | 68 | * <pre class="code">
|
65 |
| - * class ExampleHandler implements WebSocketHandler { |
66 |
| - * |
67 |
| - * @Override |
68 |
| - * public Mono<Void> handle(WebSocketSession session) { |
69 |
| - * |
70 |
| - * Mono<Void> input = session.receive() |
71 |
| - * .doOnNext(message -> { |
72 |
| - * // ... |
73 |
| - * }) |
74 |
| - * .concatMap(message -> { |
75 |
| - * // ... |
76 |
| - * }) |
77 |
| - * .then(); |
78 |
| - * |
79 |
| - * Flux<String> source = ... ; |
80 |
| - * Mono<Void> output = session.send(source.map(session::textMessage)); |
81 |
| - * |
82 |
| - * return Mono.zip(input, output).then(); |
83 |
| - * } |
84 |
| - * } |
| 69 | + * class ExampleHandler implements WebSocketHandler { |
| 70 | + * |
| 71 | + * @Override |
| 72 | + * public Mono<Void> handle(WebSocketSession session) { |
| 73 | + * |
| 74 | + * Mono<Void> input = session.receive() |
| 75 | + * .doOnNext(message -> { |
| 76 | + * // ... |
| 77 | + * }) |
| 78 | + * .concatMap(message -> { |
| 79 | + * // ... |
| 80 | + * }) |
| 81 | + * .then(); |
| 82 | + * |
| 83 | + * Flux<String> source = ... ; |
| 84 | + * Mono<Void> output = session.send(source.map(session::textMessage)); |
| 85 | + * |
| 86 | + * return Mono.zip(input, output).then(); |
| 87 | + * } |
| 88 | + * } |
85 | 89 | * </pre>
|
86 | 90 | *
|
87 | 91 | * <p>A {@code WebSocketHandler} must compose the inbound and outbound streams
|
|
0 commit comments