diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/AbstractHttpSendingTransportHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/AbstractHttpSendingTransportHandler.java index 73aac7449e4d..9a2f75d1ba08 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/AbstractHttpSendingTransportHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/AbstractHttpSendingTransportHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -79,15 +79,7 @@ else if (sockJsSession.isClosed()) { if (logger.isDebugEnabled()) { logger.debug("Connection already closed (but not removed yet) for " + sockJsSession); } - SockJsFrameFormat frameFormat = this.getFrameFormat(request); - SockJsFrame frame = SockJsFrame.closeFrameGoAway(); - String formattedFrame = frameFormat.format(frame); - try { - response.getBody().write(formattedFrame.getBytes(SockJsFrame.CHARSET)); - } - catch (IOException ex) { - throw new SockJsException("Failed to send " + frame, sockJsSession.getId(), ex); - } + writeFrame(SockJsFrame.closeFrameGoAway(), request, response, sockJsSession); } else if (!sockJsSession.isActive()) { if (logger.isTraceEnabled()) { @@ -99,13 +91,19 @@ else if (!sockJsSession.isActive()) { if (logger.isDebugEnabled()) { logger.debug("Another " + getTransportType() + " connection still open for " + sockJsSession); } - String formattedFrame = getFrameFormat(request).format(SockJsFrame.closeFrameAnotherConnectionOpen()); - try { - response.getBody().write(formattedFrame.getBytes(SockJsFrame.CHARSET)); - } - catch (IOException ex) { - throw new SockJsException("Failed to send " + formattedFrame, sockJsSession.getId(), ex); - } + writeFrame(SockJsFrame.closeFrameAnotherConnectionOpen(), request, response, sockJsSession); + } + } + + private void writeFrame(SockJsFrame frame, ServerHttpRequest request, ServerHttpResponse response, + AbstractHttpSockJsSession sockJsSession) { + + String formattedFrame = getFrameFormat(request).format(frame); + try { + response.getBody().write(formattedFrame.getBytes(SockJsFrame.CHARSET)); + } + catch (IOException ex) { + throw new SockJsException("Failed to send " + formattedFrame, sockJsSession.getId(), ex); } } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java index ab90363c7f1c..04372fb52bf5 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.