英文:
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<MessageConverter> messageConverters) {
messageConverters.add(new ByteArrayMessageConverter());
return false;
}
@Override
protected boolean sameOriginDisabled() {
return true;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论