Sending BinaryMessage from java WebSocketStompClient

According to ( Is it possible to send binary data with STOMP over WebSockets using Spring-WebSockets? ) It is possible that it is possible to send binary messages with STOMP over websites.

I configured the WebSocketStompClient like this:

List<Transport> transports = new ArrayList<>(1);
transports.add(new WebSocketTransport( new StandardWebSocketClient()) );
WebSocketClient transport = new SockJsClient(transports);
WebSocketStompClient stompClient = new WebSocketStompClient(transport);
// using a custom converter to serialize java object to byte[]
stompClient.setMessageConverter( new SerializedJavaObjectMessageConverter() );

      

I use the following to send a message:

StompHeaders stompHeaders = new StompHeaders();
stompHeaders.setDestination( "/app/greetings");
stompHeaders.setContentType( MimeTypeUtils.APPLICATION_OCTET_STREAM );
session.send(stompHeaders, new Greeting( message ));

      

I get an exception from the following statement:

Assert.isInstanceOf(TextMessage.class, message, this + " supports text messages only.");

      

found in org.springframework.web.socket.sockjs.client.AbstractClientSockJs (below) which appears to support TextMessage:

@Override
public final void sendMessage(WebSocketMessage<?> message) throws IOException {
    Assert.state(State.OPEN.equals(this.state), this + " is not open, current state=" + this.state);
    Assert.isInstanceOf(TextMessage.class, message, this + " supports text messages only.");
    String payload = ((TextMessage) message).getPayload();
    payload = getMessageCodec().encode(new String[] { payload });
    payload = payload.substring(1); // the client-side doesn't need message framing (letter "a")
    message = new TextMessage(payload);
    if (logger.isTraceEnabled()) {
            logger.trace("Sending message " + message + " in " + this);
    }
    sendInternal((TextMessage) message);
}

      

Does the new java client WebSocketStompClient (4.2-SNAPSHOT) as above (i.e. using SockJS for backup) support sending binary messages?

UPDATE:

The following client configuration works:

WebSocketClient transport = new StandardWebSocketClient();
WebSocketStompClient stompClient = new WebSocketStompClient(transport)

      

+3


source to share


1 answer


I managed to get this to work by sending a TextMessage instead. I created another custom MessageConverter that Base64 encodes / decodes a serialized object:



public class Base64JavaObjectMessageConverter extends AbstractMessageConverter {

    public Base64JavaObjectMessageConverter() {
        super(MimeTypeUtils.TEXT_PLAIN);
    }

    @Override
    protected boolean supports(Class<?> clazz) {
        return true;
    }

    @Override
    public Object convertFromInternal(Message<?> message, Class<?> targetClass) {
        byte[] messageBytes = Base64.getDecoder().decode( (byte[])message.getPayload() );
        return SerializationUtils.deserialize( messageBytes  );
    }

    @Override
    public Object convertToInternal(Object payload, MessageHeaders headers) {
        byte[] messageBytes = SerializationUtils.serialize( payload );
        return Base64.getEncoder().encode( messageBytes);
    }

}

      

+1


source







All Articles