@@ -16,69 +16,12 @@ Creating a WebSocket server is as simple as implementing `WebSocketHandler` or,
16
16
likely, extending either `TextWebSocketHandler` or `BinaryWebSocketHandler`. The following
17
17
example uses `TextWebSocketHandler`:
18
18
19
- [source,java,indent=0,subs="verbatim,quotes"]
20
- ----
21
- import org.springframework.web.socket.WebSocketHandler;
22
- import org.springframework.web.socket.WebSocketSession;
23
- import org.springframework.web.socket.TextMessage;
24
-
25
- public class MyHandler extends TextWebSocketHandler {
26
-
27
- @Override
28
- public void handleTextMessage(WebSocketSession session, TextMessage message) {
29
- // ...
30
- }
31
-
32
- }
33
- ----
19
+ include-code::./MyHandler[tag=snippet,indent=0]
34
20
35
- There is dedicated WebSocket Java configuration and XML namespace support for mapping the preceding
21
+ There is dedicated WebSocket programmatic configuration and XML namespace support for mapping the preceding
36
22
WebSocket handler to a specific URL, as the following example shows:
37
23
38
- [source,java,indent=0,subs="verbatim,quotes"]
39
- ----
40
- import org.springframework.web.socket.config.annotation.EnableWebSocket;
41
- import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
42
- import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
43
-
44
- @Configuration
45
- @EnableWebSocket
46
- public class WebSocketConfig implements WebSocketConfigurer {
47
-
48
- @Override
49
- public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
50
- registry.addHandler(myHandler(), "/myHandler");
51
- }
52
-
53
- @Bean
54
- public WebSocketHandler myHandler() {
55
- return new MyHandler();
56
- }
57
-
58
- }
59
- ----
60
-
61
- The following example shows the XML configuration equivalent of the preceding example:
62
-
63
- [source,xml,indent=0,subs="verbatim,quotes,attributes"]
64
- ----
65
- <beans xmlns="http://www.springframework.org/schema/beans"
66
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
67
- xmlns:websocket="http://www.springframework.org/schema/websocket"
68
- xsi:schemaLocation="
69
- http://www.springframework.org/schema/beans
70
- https://www.springframework.org/schema/beans/spring-beans.xsd
71
- http://www.springframework.org/schema/websocket
72
- https://www.springframework.org/schema/websocket/spring-websocket.xsd">
73
-
74
- <websocket:handlers>
75
- <websocket:mapping path="/myHandler" handler="myHandler"/>
76
- </websocket:handlers>
77
-
78
- <bean id="myHandler" class="org.springframework.samples.MyHandler"/>
79
-
80
- </beans>
81
- ----
24
+ include-code::./WebSocketConfiguration[tag=snippet,indent=0]
82
25
83
26
The preceding example is for use in Spring MVC applications and should be included
84
27
in the configuration of a xref:web/webmvc/mvc-servlet.adoc[`DispatcherServlet`]. However, Spring's
@@ -104,45 +47,7 @@ You can use such an interceptor to preclude the handshake or to make any attribu
104
47
available to the `WebSocketSession`. The following example uses a built-in interceptor
105
48
to pass HTTP session attributes to the WebSocket session:
106
49
107
- [source,java,indent=0,subs="verbatim,quotes"]
108
- ----
109
- @Configuration
110
- @EnableWebSocket
111
- public class WebSocketConfig implements WebSocketConfigurer {
112
-
113
- @Override
114
- public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
115
- registry.addHandler(new MyHandler(), "/myHandler")
116
- .addInterceptors(new HttpSessionHandshakeInterceptor());
117
- }
118
-
119
- }
120
- ----
121
-
122
- The following example shows the XML configuration equivalent of the preceding example:
123
-
124
- [source,xml,indent=0,subs="verbatim,quotes,attributes"]
125
- ----
126
- <beans xmlns="http://www.springframework.org/schema/beans"
127
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
128
- xmlns:websocket="http://www.springframework.org/schema/websocket"
129
- xsi:schemaLocation="
130
- http://www.springframework.org/schema/beans
131
- https://www.springframework.org/schema/beans/spring-beans.xsd
132
- http://www.springframework.org/schema/websocket
133
- https://www.springframework.org/schema/websocket/spring-websocket.xsd">
134
-
135
- <websocket:handlers>
136
- <websocket:mapping path="/myHandler" handler="myHandler"/>
137
- <websocket:handshake-interceptors>
138
- <bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor"/>
139
- </websocket:handshake-interceptors>
140
- </websocket:handlers>
141
-
142
- <bean id="myHandler" class="org.springframework.samples.MyHandler"/>
143
-
144
- </beans>
145
- ----
50
+ include-code::./WebSocketConfiguration[tag=snippet,indent=0]
146
51
147
52
A more advanced option is to extend the `DefaultHandshakeHandler` that performs
148
53
the steps of the WebSocket handshake, including validating the client origin,
@@ -236,69 +141,17 @@ You can configure of the underlying WebSocket server such as input message buffe
236
141
idle timeout, and more.
237
142
238
143
For Jakarta WebSocket servers, you can add a `ServletServerContainerFactoryBean` to your
239
- Java configuration. For example:
240
-
241
- [source,java,indent=0,subs="verbatim,quotes"]
242
- ----
243
- @Bean
244
- public ServletServerContainerFactoryBean createWebSocketContainer() {
245
- ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
246
- container.setMaxTextMessageBufferSize(8192);
247
- container.setMaxBinaryMessageBufferSize(8192);
248
- return container;
249
- }
250
- ----
251
-
252
- Or to your XML configuration:
144
+ configuration. For example:
253
145
254
- [source,xml,indent=0,subs="verbatim,quotes,attributes"]
255
- ----
256
- <beans xmlns="http://www.springframework.org/schema/beans"
257
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
258
- xmlns:websocket="http://www.springframework.org/schema/websocket"
259
- xsi:schemaLocation="
260
- http://www.springframework.org/schema/beans
261
- https://www.springframework.org/schema/beans/spring-beans.xsd
262
- http://www.springframework.org/schema/websocket
263
- https://www.springframework.org/schema/websocket/spring-websocket.xsd">
264
-
265
- <bean class="org.springframework...ServletServerContainerFactoryBean">
266
- <property name="maxTextMessageBufferSize" value="8192"/>
267
- <property name="maxBinaryMessageBufferSize" value="8192"/>
268
- </bean>
269
-
270
- </beans>
271
- ----
146
+ include-code::./WebSocketConfiguration[tag=snippet,indent=0]
272
147
273
148
NOTE: For client Jakarta WebSocket configuration, use
274
- ContainerProvider.getWebSocketContainer() in Java configuration, or
149
+ ContainerProvider.getWebSocketContainer() in programmatic configuration, or
275
150
`WebSocketContainerFactoryBean` in XML.
276
151
277
- For Jetty, you can supply a `Consumer` callback to configure the WebSocket server:
152
+ For Jetty, you can supply a callback to configure the WebSocket server:
278
153
279
- [source,java,indent=0,subs="verbatim,quotes"]
280
- ----
281
- @Configuration
282
- @EnableWebSocket
283
- public class WebSocketConfig implements WebSocketConfigurer {
284
-
285
- @Override
286
- public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
287
- registry.addHandler(echoWebSocketHandler(), "/echo").setHandshakeHandler(handshakeHandler());
288
- }
289
-
290
- @Bean
291
- public DefaultHandshakeHandler handshakeHandler() {
292
- JettyRequestUpgradeStrategy strategy = new JettyRequestUpgradeStrategy();
293
- strategy.addWebSocketConfigurer(configurable -> {
294
- policy.setInputBufferSize(8192);
295
- policy.setIdleTimeout(600000);
296
- });
297
- return new DefaultHandshakeHandler(strategy);
298
- }
299
-
300
- }
301
- ----
154
+ include-code::./JettyWebSocketConfiguration[tag=snippet,indent=0]
302
155
303
156
TIP: When using STOMP over WebSocket, you will also need to configure
304
157
xref:web/websocket/stomp/server-config.adoc[STOMP WebSocket transport]
@@ -331,51 +184,4 @@ The three possible behaviors are:
331
184
332
185
You can configure WebSocket and SockJS allowed origins, as the following example shows:
333
186
334
- [source,java,indent=0,subs="verbatim,quotes"]
335
- ----
336
- import org.springframework.web.socket.config.annotation.EnableWebSocket;
337
- import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
338
- import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
339
-
340
- @Configuration
341
- @EnableWebSocket
342
- public class WebSocketConfig implements WebSocketConfigurer {
343
-
344
- @Override
345
- public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
346
- registry.addHandler(myHandler(), "/myHandler").setAllowedOrigins("https://mydomain.com");
347
- }
348
-
349
- @Bean
350
- public WebSocketHandler myHandler() {
351
- return new MyHandler();
352
- }
353
-
354
- }
355
- ----
356
-
357
- The following example shows the XML configuration equivalent of the preceding example:
358
-
359
- [source,xml,indent=0,subs="verbatim,quotes,attributes"]
360
- ----
361
- <beans xmlns="http://www.springframework.org/schema/beans"
362
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
363
- xmlns:websocket="http://www.springframework.org/schema/websocket"
364
- xsi:schemaLocation="
365
- http://www.springframework.org/schema/beans
366
- https://www.springframework.org/schema/beans/spring-beans.xsd
367
- http://www.springframework.org/schema/websocket
368
- https://www.springframework.org/schema/websocket/spring-websocket.xsd">
369
-
370
- <websocket:handlers allowed-origins="https://mydomain.com">
371
- <websocket:mapping path="/myHandler" handler="myHandler" />
372
- </websocket:handlers>
373
-
374
- <bean id="myHandler" class="org.springframework.samples.MyHandler"/>
375
-
376
- </beans>
377
- ----
378
-
379
-
380
-
381
-
187
+ include-code::./WebSocketConfiguration[tag=snippet,indent=0]
0 commit comments