Skip to content

Commit cb785e1

Browse files
alexjansonsAleksandrs Jansons
authored andcommitted
Ensure WebSocket disconnect msg reaches the client
In some application setups, the WebSocket server does not transmit the disconnect message to the client, so that the client has no idea that the established connection has been terminated. This issue arises when the application uses SimpleBrokerMessageHandler and the error handler is set to the instance of StompSubProtocolErrorHandler or an extended class that does not override the handleErrorMessageToClient method. The commit fixes disconnect message population so that `java.lang.IllegalArgumentException: No StompHeaderAccessor` exception is not thrown in the handleErrorMessageToClient method in StompSubProtocolErrorHandler class.
1 parent 1de36ab commit cb785e1

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,9 @@ else if (StompCommand.CONNECTED.equals(command)) {
465465
}
466466

467467
if (StompCommand.ERROR.equals(command) && getErrorHandler() != null) {
468-
Message<byte[]> errorMessage = getErrorHandler().handleErrorMessageToClient((Message<byte[]>) message);
468+
Message<byte[]> enrichedMessage =
469+
MessageBuilder.createMessage((byte[]) message.getPayload(), accessor.getMessageHeaders());
470+
Message<byte[]> errorMessage = getErrorHandler().handleErrorMessageToClient(enrichedMessage);
469471
if (errorMessage != null) {
470472
accessor = MessageHeaderAccessor.getAccessor(errorMessage, StompHeaderAccessor.class);
471473
Assert.state(accessor != null, "No StompHeaderAccessor");

0 commit comments

Comments
 (0)