无法使用SimpMessagingTemplate发送二进制数据(byte[])。

huangapple go评论91阅读模式
英文:

Unable to Send Binary Data (byte[]) using SimpMessagingTemplate

问题

我可以使用SimpMessagingTemplate.convertAndSend()方法将字符串消息发送到WebSocket,但是当我尝试发送byte[]时却无法工作。当我将byte[]发送到WebSocket订阅通道时,会触发WebSocket断开连接事件并且连接丢失。有没有办法使用SimpMessagingTemplate发送byte[]到WebSocket!!!!

@Autowired
private SimpMessagingTemplate template;

String body = "要发送的消息";

template.convertAndSend("/channel", body);   --------- 可行

template.convertAndSend("/channel", body.getBytes());   --------- 不可行
英文:

I am able to send string messages to websocket using SimpMessagingTemplate.convertAndSend() method, but same is not working when I try to send byte[]. When I send byte[] to the websocket subscription channel, a websocket DISCONNECT event is getting triggered and connection is getting lost. Any Idea to send byte[] to websocket using SimpMessagingTemplate !!!!!

@Autowired
private SimpMessagingTemplate template;

String body = "Message to be Sent";

template.convertAndSend("/channel", body);   --------- working

template.convertAndSend("/channel", body.getBytes());   --------- Not working

答案1

得分: 1

你尝试过向字节数组添加自定义消息转换器吗?只需重写configureMessageConverters方法。

@Configuration
public class WebSocketSecurityConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {

    @Override
    protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
        messages.simpTypeMatchers(
                SimpMessageType.DISCONNECT, SimpMessageType.OTHER).permitAll();
        messages.anyMessage().authenticated();
    }

    @Override
    public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
        messageConverters.add(new ByteArrayMessageConverter());
        return false;
    }

    @Override
    protected boolean sameOriginDisabled() {
        return true;
    }
}
英文:

Have you tried adding a custom message converter to the byte array?. Just override configureMessageConverters method.

@Configuration
public class WebSocketSecurityConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {

    @Override
    protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
        messages.simpTypeMatchers(
                SimpMessageType.DISCONNECT, SimpMessageType.OTHER).permitAll();
        messages.anyMessage().authenticated();
    }

    @Override
    public boolean configureMessageConverters(List&lt;MessageConverter&gt; messageConverters) {
        messageConverters.add(new ByteArrayMessageConverter());
        return false;
    }

    @Override
    protected boolean sameOriginDisabled() {
        return true;
    }
}

huangapple
  • 本文由 发表于 2020年9月15日 18:04:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63899558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定