Is it possible to send binary data with STOMP over WebSockets using Spring-WebSockets?

后端 未结 2 917
失恋的感觉
失恋的感觉 2020-12-15 10:07

I am able to send and receive JSON with STOMP over WebSockets following spring documentation. However performance is poor at large high rates, so I wish to profile the use o

2条回答
  •  无人及你
    2020-12-15 10:56

    Try to configure you Server just with ByteArrayMessageConverter:

    @Configuration
    @EnableWebSocketMessageBroker
    public class MyWebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
       
       @Override
        public boolean configureMessageConverters(List messageConverters) {
            messageConverters.add(new ByteArrayMessageConverter());
            return false;
        }
    
    }
    

    UPDATE

    Ah! I see that. Thanks:

    public TextMessage(byte[] payload) {
        super(new String(payload, UTF_8));
        this.bytes = payload;
    }
    

    From other side from STOMP spec:

    The body of a STOMP message must be a String. If you want to send and receive JSON objects, ...

    According to the ArrayBuffer:

    Getting an array buffer from existing data

    • From a Base64 string
    • From a local file

    So, I think you don't have a chioce rather than provide you some custom MessageConverter, which converts your byte[] to Base64 String and send it.

    On the JavaScript side you should decode that string to the ArrayBuffer somehow.

提交回复
热议问题