@Override protected void disconnect(CloseStatus status) throws IOException { if (this.webSocketSession != null) { this.webSocketSession.close(status); } }
@Override protected void closeConnection() throws Exception { if (this.webSocketSession != null) { this.webSocketSession.close(); } }
@Override public void close(CloseStatus status) throws IOException { this.delegate.close(status); }
@Override public void close() throws IOException { this.delegate.close(); }
@Override protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) { try { session.close(CloseStatus.NOT_ACCEPTABLE.withReason("Binary messages not supported")); } catch (IOException ex) { // ignore } }
@Override protected void handleTextMessage(WebSocketSession session, TextMessage message) { try { session.close(CloseStatus.NOT_ACCEPTABLE.withReason("Text messages not supported")); } catch (IOException ex) { // ignore } }
public static void tryCloseWithError(WebSocketSession session, Throwable exception, Log logger) { if (logger.isErrorEnabled()) { logger.error("Closing session due to exception for " + session, exception); } if (session.isOpen()) { try { session.close(CloseStatus.SERVER_ERROR); } catch (Throwable ex) { // ignore } } }
@Override public void close() { WebSocketSession session = this.session; if (session != null) { try { session.close(); } catch (IOException ex) { if (logger.isDebugEnabled()) { logger.debug("Failed to close session: " + session.getId(), ex); } } } } }
@Override protected void disconnect(CloseStatus status) throws IOException { if (isActive()) { synchronized (this.disconnectLock) { if (isActive()) { this.disconnected = true; if (this.webSocketSession != null) { this.webSocketSession.close(status); } } } } }
@Override public final void stop() { synchronized (this.lifecycleMonitor) { this.running = false; this.clientOutboundChannel.unsubscribe(this); } // Proactively notify all active WebSocket sessions for (WebSocketSessionHolder holder : this.sessions.values()) { try { holder.getSession().close(CloseStatus.GOING_AWAY); } catch (Throwable ex) { if (logger.isWarnEnabled()) { logger.warn("Failed to close '" + holder.getSession() + "': " + ex); } } } }
@After public void teardown() throws Exception { this.wsSession.close(); this.client.stop(); this.server.stop(); }
@Test public void closeStatusNormal() throws Exception { BlockingSession session = new BlockingSession(); session.setOpen(true); WebSocketSession decorator = new ConcurrentWebSocketSessionDecorator(session, 10 * 1000, 1024); decorator.close(CloseStatus.PROTOCOL_ERROR); assertEquals(CloseStatus.PROTOCOL_ERROR, session.getCloseStatus()); decorator.close(CloseStatus.SERVER_ERROR); assertEquals("Should have been ignored", CloseStatus.PROTOCOL_ERROR, session.getCloseStatus()); }
@Test public void registerWebSocketHandlerWithSockJS() throws Exception { WebSocketSession session = this.webSocketClient.doHandshake( new AbstractWebSocketHandler() {}, getWsBaseUrl() + "/sockjs/websocket").get(); TestHandler serverHandler = this.wac.getBean(TestHandler.class); assertTrue(serverHandler.connectLatch.await(2, TimeUnit.SECONDS)); session.close(); }
@Test public void registerWebSocketHandler() throws Exception { WebSocketSession session = this.webSocketClient.doHandshake( new AbstractWebSocketHandler() {}, getWsBaseUrl() + "/ws").get(); TestHandler serverHandler = this.wac.getBean(TestHandler.class); assertTrue(serverHandler.connectLatch.await(2, TimeUnit.SECONDS)); session.close(); }
@Test(timeout = 5000) public void fallbackAfterConnectTimeout() throws Exception { TestClientHandler clientHandler = new TestClientHandler(); this.testFilter.sleepDelayMap.put("/xhr_streaming", 10000L); this.testFilter.sendErrorMap.put("/xhr_streaming", 503); initSockJsClient(createXhrTransport()); this.sockJsClient.setConnectTimeoutScheduler(this.wac.getBean(ThreadPoolTaskScheduler.class)); WebSocketSession clientSession = sockJsClient.doHandshake(clientHandler, this.baseUrl + "/echo").get(); assertEquals("Fallback didn't occur", XhrClientSockJsSession.class, clientSession.getClass()); TextMessage message = new TextMessage("message1"); clientSession.sendMessage(message); clientHandler.awaitMessage(message, 5000); clientSession.close(); }
@Test public void subProtocolNegotiation() throws Exception { WebSocketHttpHeaders headers = new WebSocketHttpHeaders(); headers.setSecWebSocketProtocol("foo"); URI url = new URI(getWsBaseUrl() + "/ws"); WebSocketSession session = this.webSocketClient.doHandshake(new TextWebSocketHandler(), headers, url).get(); assertEquals("foo", session.getAcceptedProtocol()); session.close(); }
@Test public void sendMessageToController() throws Exception { TextMessage message = create(StompCommand.SEND).headers("destination:/app/simple").build(); WebSocketSession session = doHandshake(new TestClientWebSocketHandler(0, message), "/ws").get(); SimpleController controller = this.wac.getBean(SimpleController.class); try { assertTrue(controller.latch.await(TIMEOUT, TimeUnit.SECONDS)); } finally { session.close(); } }
@Test // SPR-11648 public void sendSubscribeToControllerAndReceiveReply() throws Exception { TextMessage m0 = create(StompCommand.CONNECT).headers("accept-version:1.1").build(); String destHeader = "destination:/app/number"; TextMessage m1 = create(StompCommand.SUBSCRIBE).headers("id:subs1", destHeader).build(); TestClientWebSocketHandler clientHandler = new TestClientWebSocketHandler(2, m0, m1); WebSocketSession session = doHandshake(clientHandler, "/ws").get(); try { assertTrue(clientHandler.latch.await(TIMEOUT, TimeUnit.SECONDS)); String payload = clientHandler.actual.get(1).getPayload(); assertTrue("Expected STOMP destination=/app/number, got " + payload, payload.contains(destHeader)); assertTrue("Expected STOMP Payload=42, got " + payload, payload.contains("42")); } finally { session.close(); } }
@Test // SPR-10930 public void sendMessageToBrokerAndReceiveReplyViaTopic() throws Exception { TextMessage m0 = create(StompCommand.CONNECT).headers("accept-version:1.1").build(); TextMessage m1 = create(StompCommand.SUBSCRIBE).headers("id:subs1", "destination:/topic/foo").build(); TextMessage m2 = create(StompCommand.SEND).headers("destination:/topic/foo").body("5").build(); TestClientWebSocketHandler clientHandler = new TestClientWebSocketHandler(2, m0, m1, m2); WebSocketSession session = doHandshake(clientHandler, "/ws").get(); try { assertTrue(clientHandler.latch.await(TIMEOUT, TimeUnit.SECONDS)); String payload = clientHandler.actual.get(1).getPayload(); assertTrue("Expected STOMP MESSAGE, got " + payload, payload.startsWith("MESSAGE\n")); } finally { session.close(); } }
@Test public void sendMessageToControllerAndReceiveReplyViaTopic() throws Exception { TextMessage m0 = create(StompCommand.CONNECT).headers("accept-version:1.1").build(); TextMessage m1 = create(StompCommand.SUBSCRIBE) .headers("id:subs1", "destination:/topic/increment").build(); TextMessage m2 = create(StompCommand.SEND) .headers("destination:/app/increment").body("5").build(); TestClientWebSocketHandler clientHandler = new TestClientWebSocketHandler(2, m0, m1, m2); WebSocketSession session = doHandshake(clientHandler, "/ws").get(); try { assertTrue(clientHandler.latch.await(TIMEOUT, TimeUnit.SECONDS)); } finally { session.close(); } }