private void scheduleSessionTask() { synchronized (this.sessions) { if (this.sessionCleanupTask != null) { return; } this.sessionCleanupTask = getTaskScheduler().scheduleAtFixedRate(() -> { List<String> removedIds = new ArrayList<>(); for (SockJsSession session : this.sessions.values()) { try { if (session.getTimeSinceLastActive() > getDisconnectDelay()) { this.sessions.remove(session.getId()); removedIds.add(session.getId()); session.close(); } } catch (Throwable ex) { // Could be part of normal workflow (e.g. browser tab closed) logger.debug("Failed to close " + session, ex); } } if (logger.isDebugEnabled() && !removedIds.isEmpty()) { logger.debug("Closed " + removedIds.size() + " sessions: " + removedIds); } }, getDisconnectDelay()); } }
@Override public void handleRequest(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException { WebSocketServerSockJsSession sockJsSession = (WebSocketServerSockJsSession) wsSession; try { wsHandler = new SockJsWebSocketHandler(getServiceConfig(), wsHandler, sockJsSession); this.handshakeHandler.doHandshake(request, response, wsHandler, sockJsSession.getAttributes()); } catch (Throwable ex) { sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR); throw new SockJsTransportFailureException("WebSocket handshake failure", wsSession.getId(), ex); } }
@Test public void handleMessageToClientWithHeartbeatSuppressingSockJsHeartbeat() throws IOException { SockJsSession sockJsSession = Mockito.mock(SockJsSession.class); when(sockJsSession.getId()).thenReturn("s1"); StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECTED); accessor.setHeartbeat(0, 10); Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders()); this.protocolHandler.handleMessageToClient(sockJsSession, message); verify(sockJsSession).getId(); verify(sockJsSession).getPrincipal(); verify(sockJsSession).disableHeartbeat(); verify(sockJsSession).sendMessage(any(WebSocketMessage.class)); verifyNoMoreInteractions(sockJsSession); sockJsSession = Mockito.mock(SockJsSession.class); when(sockJsSession.getId()).thenReturn("s1"); accessor = StompHeaderAccessor.create(StompCommand.CONNECTED); accessor.setHeartbeat(0, 0); message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders()); this.protocolHandler.handleMessageToClient(sockJsSession, message); verify(sockJsSession).getId(); verify(sockJsSession).getPrincipal(); verify(sockJsSession).sendMessage(any(WebSocketMessage.class)); verifyNoMoreInteractions(sockJsSession); }
private void scheduleSessionTask() { synchronized (this.sessions) { if (this.sessionCleanupTask != null) { return; } this.sessionCleanupTask = getTaskScheduler().scheduleAtFixedRate(() -> { List<String> removedIds = new ArrayList<>(); for (SockJsSession session : this.sessions.values()) { try { if (session.getTimeSinceLastActive() > getDisconnectDelay()) { this.sessions.remove(session.getId()); removedIds.add(session.getId()); session.close(); } } catch (Throwable ex) { // Could be part of normal workflow (e.g. browser tab closed) logger.debug("Failed to close " + session, ex); } } if (logger.isDebugEnabled() && !removedIds.isEmpty()) { logger.debug("Closed " + removedIds.size() + " sessions: " + removedIds); } }, getDisconnectDelay()); } }
private void scheduleSessionTask() { synchronized (this.sessions) { if (this.sessionCleanupTask != null) { return; } this.sessionCleanupTask = getTaskScheduler().scheduleAtFixedRate(() -> { List<String> removedIds = new ArrayList<>(); for (SockJsSession session : this.sessions.values()) { try { if (session.getTimeSinceLastActive() > getDisconnectDelay()) { this.sessions.remove(session.getId()); removedIds.add(session.getId()); session.close(); } } catch (Throwable ex) { // Could be part of normal workflow (e.g. browser tab closed) logger.debug("Failed to close " + session, ex); } } if (logger.isDebugEnabled() && !removedIds.isEmpty()) { logger.debug("Closed " + removedIds.size() + " sessions: " + removedIds); } }, getDisconnectDelay()); } }
@Override public void handleRequest(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException { WebSocketServerSockJsSession sockJsSession = (WebSocketServerSockJsSession) wsSession; try { wsHandler = new SockJsWebSocketHandler(getServiceConfig(), wsHandler, sockJsSession); this.handshakeHandler.doHandshake(request, response, wsHandler, sockJsSession.getAttributes()); } catch (Throwable ex) { sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR); throw new SockJsTransportFailureException("WebSocket handshake failure", wsSession.getId(), ex); } }
@Override public void handleRequest(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException { WebSocketServerSockJsSession sockJsSession = (WebSocketServerSockJsSession) wsSession; try { wsHandler = new SockJsWebSocketHandler(getServiceConfig(), wsHandler, sockJsSession); this.handshakeHandler.doHandshake(request, response, wsHandler, sockJsSession.getAttributes()); } catch (Throwable ex) { sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR); throw new SockJsTransportFailureException("WebSocket handshake failure", wsSession.getId(), ex); } }