Skip to content

Commit

Permalink
Allow sending binary messages with STOMP
Browse files Browse the repository at this point in the history
After this change the StompSubProtocolHandler sends STOMP frames
with content-type=octet-stream as a binary WebSocket message.

Issue: SPR-12475
  • Loading branch information
rstoyanchev committed Dec 23, 2014
1 parent 6b3023c commit 670974d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.messaging.support.MessageHeaderInitializer;
import org.springframework.util.Assert;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
Expand Down Expand Up @@ -355,7 +357,12 @@ else if (StompCommand.CONNECTED.equals(command)) {
}
try {
byte[] bytes = this.stompEncoder.encode(stompAccessor.getMessageHeaders(), (byte[]) message.getPayload());
session.sendMessage(new TextMessage(bytes));
if (MimeTypeUtils.APPLICATION_OCTET_STREAM.isCompatibleWith(stompAccessor.getContentType())) {
session.sendMessage(new BinaryMessage(bytes));
}
else {
session.sendMessage(new TextMessage(bytes));
}
}
catch (SessionLimitExceededException ex) {
// Bad session, just get out
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.core.NestedCheckedException;
import org.springframework.util.Assert;
import org.springframework.web.socket.CloseStatus;
Expand Down Expand Up @@ -158,9 +157,7 @@ public Map<String, Object> getAttributes() {

public final void sendMessage(WebSocketMessage<?> message) throws IOException {
Assert.state(!isClosed(), "Cannot send a message when session is closed");
if (!(message instanceof TextMessage)) {
throw new IllegalArgumentException("Expected text message: " + message);
}
Assert.isInstanceOf(TextMessage.class, message, "SockJS supports text messages only: " + message);
sendMessageInternal(((TextMessage) message).getPayload());
}

Expand Down

0 comments on commit 670974d

Please sign in to comment.