Skip to content

Commit 2b1ff4c

Browse files
committed
Fix condition in SubProtocolWebSocketHandler
Issue: SPR-11884
1 parent 9880d2b commit 2b1ff4c

File tree

2 files changed

+4
-7
lines changed

2 files changed

+4
-7
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public class SubProtocolWebSocketHandler implements WebSocketHandler,
7070
* connection isn't doing well (proxy issue, slow network?) and can be closed.
7171
* @see #checkSessions()
7272
*/
73-
private final int TIME_TO_FIRST_MESSAGE = 60 * 1000;
73+
private static final int TIME_TO_FIRST_MESSAGE = 60 * 1000;
7474

7575

7676
private final Log logger = LogFactory.getLog(SubProtocolWebSocketHandler.class);
@@ -298,10 +298,6 @@ public void handleMessage(WebSocketSession session, WebSocketMessage<?> message)
298298
if (holder != null) {
299299
holder.setHasHandledMessages();
300300
}
301-
else {
302-
// Should never happen
303-
throw new IllegalStateException("Session not found: " + session);
304-
}
305301
checkSessions();
306302
}
307303

@@ -366,7 +362,7 @@ private String resolveSessionId(Message<?> message) {
366362
*/
367363
private void checkSessions() throws IOException {
368364
long currentTime = System.currentTimeMillis();
369-
if (!isRunning() && currentTime - this.lastSessionCheckTime < TIME_TO_FIRST_MESSAGE) {
365+
if (!isRunning() || (currentTime - this.lastSessionCheckTime < TIME_TO_FIRST_MESSAGE)) {
370366
return;
371367
}
372368
if (this.sessionCheckLock.tryLock()) {
@@ -376,7 +372,7 @@ private void checkSessions() throws IOException {
376372
continue;
377373
}
378374
long timeSinceCreated = currentTime - holder.getCreateTime();
379-
if (holder.hasHandledMessages() || timeSinceCreated < TIME_TO_FIRST_MESSAGE) {
375+
if (timeSinceCreated < TIME_TO_FIRST_MESSAGE) {
380376
continue;
381377
}
382378
WebSocketSession session = holder.getSession();

spring-websocket/src/test/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandlerTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ public void checkSession() throws Exception {
165165
new DirectFieldAccessor(sessions.get("id1")).setPropertyValue("createTime", sixtyOneSecondsAgo);
166166
new DirectFieldAccessor(sessions.get("id2")).setPropertyValue("createTime", sixtyOneSecondsAgo);
167167

168+
this.webSocketHandler.start();
168169
this.webSocketHandler.handleMessage(session1, new TextMessage("foo"));
169170

170171
assertTrue(session1.isOpen());

0 commit comments

Comments
 (0)