16
16
17
17
package org .springframework .web .socket .messaging ;
18
18
19
- import java .io .IOException ;
20
19
import java .util .ArrayList ;
21
20
import java .util .Collections ;
22
21
import java .util .LinkedHashSet ;
@@ -292,7 +291,7 @@ public void afterConnectionEstablished(WebSocketSession session) throws Exceptio
292
291
return ;
293
292
}
294
293
this .stats .incrementSessionCount (session );
295
- session = new ConcurrentWebSocketSessionDecorator (session , getSendTimeLimit (), getSendBufferSizeLimit () );
294
+ session = decorateSession (session );
296
295
this .sessions .put (session .getId (), new WebSocketSessionHolder (session ));
297
296
findProtocolHandler (session ).afterSessionStarted (session , this .clientInboundChannel );
298
297
}
@@ -377,6 +376,23 @@ public boolean supportsPartialMessages() {
377
376
}
378
377
379
378
379
+ /**
380
+ * Decorate the given {@link WebSocketSession}, if desired.
381
+ * <p>The default implementation builds a {@link ConcurrentWebSocketSessionDecorator}
382
+ * with the configured {@link #getSendTimeLimit() send-time limit} and
383
+ * {@link #getSendBufferSizeLimit() buffer-size limit}.
384
+ * @param session the original {@code WebSocketSession}
385
+ * @return the decorated {@code WebSocketSession}, or potentially the given session as-is
386
+ * @since 4.3.13
387
+ */
388
+ protected WebSocketSession decorateSession (WebSocketSession session ) {
389
+ return new ConcurrentWebSocketSessionDecorator (session , getSendTimeLimit (), getSendBufferSizeLimit ());
390
+ }
391
+
392
+ /**
393
+ * Find a {@link SubProtocolHandler} for the given session.
394
+ * @param session the {@code WebSocketSession} to find a handler for
395
+ */
380
396
protected final SubProtocolHandler findProtocolHandler (WebSocketSession session ) {
381
397
String protocol = null ;
382
398
try {
@@ -432,12 +448,11 @@ private String resolveSessionId(Message<?> message) {
432
448
* When a session is connected through a higher-level protocol it has a chance
433
449
* to use heartbeat management to shut down sessions that are too slow to send
434
450
* or receive messages. However, after a WebSocketSession is established and
435
- * before the higher level protocol is fully connected there is a possibility
436
- * for sessions to hang. This method checks and closes any sessions that have
437
- * been connected for more than 60 seconds without having received a single
438
- * message.
451
+ * before the higher level protocol is fully connected there is a possibility for
452
+ * sessions to hang. This method checks and closes any sessions that have been
453
+ * connected for more than 60 seconds without having received a single message.
439
454
*/
440
- private void checkSessions () throws IOException {
455
+ private void checkSessions () {
441
456
long currentTime = System .currentTimeMillis ();
442
457
if (!isRunning () || (currentTime - this .lastSessionCheckTime < TIME_TO_FIRST_MESSAGE )) {
443
458
return ;
@@ -497,12 +512,13 @@ private static class WebSocketSessionHolder {
497
512
498
513
private final WebSocketSession session ;
499
514
500
- private final long createTime = System . currentTimeMillis () ;
515
+ private final long createTime ;
501
516
502
- private volatile boolean handledMessages ;
517
+ private volatile boolean hasHandledMessages ;
503
518
504
- private WebSocketSessionHolder (WebSocketSession session ) {
519
+ public WebSocketSessionHolder (WebSocketSession session ) {
505
520
this .session = session ;
521
+ this .createTime = System .currentTimeMillis ();
506
522
}
507
523
508
524
public WebSocketSession getSession () {
@@ -514,17 +530,17 @@ public long getCreateTime() {
514
530
}
515
531
516
532
public void setHasHandledMessages () {
517
- this .handledMessages = true ;
533
+ this .hasHandledMessages = true ;
518
534
}
519
535
520
536
public boolean hasHandledMessages () {
521
- return this .handledMessages ;
537
+ return this .hasHandledMessages ;
522
538
}
523
539
524
540
@ Override
525
541
public String toString () {
526
542
return "WebSocketSessionHolder[session=" + this .session + ", createTime=" +
527
- this .createTime + ", hasHandledMessages=" + this .handledMessages + "]" ;
543
+ this .createTime + ", hasHandledMessages=" + this .hasHandledMessages + "]" ;
528
544
}
529
545
}
530
546
0 commit comments