1616
1717package org .springframework .web .socket .messaging ;
1818
19- import java .io .IOException ;
2019import java .util .ArrayList ;
2120import java .util .Collections ;
2221import java .util .LinkedHashSet ;
@@ -292,7 +291,7 @@ public void afterConnectionEstablished(WebSocketSession session) throws Exceptio
292291 return ;
293292 }
294293 this .stats .incrementSessionCount (session );
295- session = new ConcurrentWebSocketSessionDecorator (session , getSendTimeLimit (), getSendBufferSizeLimit () );
294+ session = decorateSession (session );
296295 this .sessions .put (session .getId (), new WebSocketSessionHolder (session ));
297296 findProtocolHandler (session ).afterSessionStarted (session , this .clientInboundChannel );
298297 }
@@ -377,6 +376,23 @@ public boolean supportsPartialMessages() {
377376 }
378377
379378
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+ */
380396 protected final SubProtocolHandler findProtocolHandler (WebSocketSession session ) {
381397 String protocol = null ;
382398 try {
@@ -432,12 +448,11 @@ private String resolveSessionId(Message<?> message) {
432448 * When a session is connected through a higher-level protocol it has a chance
433449 * to use heartbeat management to shut down sessions that are too slow to send
434450 * 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.
439454 */
440- private void checkSessions () throws IOException {
455+ private void checkSessions () {
441456 long currentTime = System .currentTimeMillis ();
442457 if (!isRunning () || (currentTime - this .lastSessionCheckTime < TIME_TO_FIRST_MESSAGE )) {
443458 return ;
@@ -497,12 +512,13 @@ private static class WebSocketSessionHolder {
497512
498513 private final WebSocketSession session ;
499514
500- private final long createTime = System . currentTimeMillis () ;
515+ private final long createTime ;
501516
502- private volatile boolean handledMessages ;
517+ private volatile boolean hasHandledMessages ;
503518
504- private WebSocketSessionHolder (WebSocketSession session ) {
519+ public WebSocketSessionHolder (WebSocketSession session ) {
505520 this .session = session ;
521+ this .createTime = System .currentTimeMillis ();
506522 }
507523
508524 public WebSocketSession getSession () {
@@ -514,17 +530,17 @@ public long getCreateTime() {
514530 }
515531
516532 public void setHasHandledMessages () {
517- this .handledMessages = true ;
533+ this .hasHandledMessages = true ;
518534 }
519535
520536 public boolean hasHandledMessages () {
521- return this .handledMessages ;
537+ return this .hasHandledMessages ;
522538 }
523539
524540 @ Override
525541 public String toString () {
526542 return "WebSocketSessionHolder[session=" + this .session + ", createTime=" +
527- this .createTime + ", hasHandledMessages=" + this .handledMessages + "]" ;
543+ this .createTime + ", hasHandledMessages=" + this .hasHandledMessages + "]" ;
528544 }
529545 }
530546
0 commit comments