I have a Websocket-stomp server based on Spring and its SimpleBroker implementation (not utilizing an external broker).
I would like to enable STOMP RECEIPT messages.
How I could configure my code to send these automatically?
In Spring Integration test for the STOMP protocol we have this code:
//SimpleBrokerMessageHandler doesn't support RECEIPT frame, hence we emulate it this way
@Bean
public ApplicationListener<SessionSubscribeEvent> webSocketEventListener(
final AbstractSubscribableChannel clientOutboundChannel) {
return event -> {
Message<byte[]> message = event.getMessage();
StompHeaderAccessor stompHeaderAccessor = StompHeaderAccessor.wrap(message);
if (stompHeaderAccessor.getReceipt() != null) {
stompHeaderAccessor.setHeader("stompCommand", StompCommand.RECEIPT);
stompHeaderAccessor.setReceiptId(stompHeaderAccessor.getReceipt());
clientOutboundChannel.send(
MessageBuilder.createMessage(new byte[0], stompHeaderAccessor.getMessageHeaders()));
}
};
}
https://github.com/spring-projects/spring-integration/blob/master/spring-integration-stomp/src/test/java/org/springframework/integration/stomp/inbound/StompInboundChannelAdapterWebSocketIntegrationTests.java
A solution similar to the post of Artem Bilan using a separated class to implement the listener.
@Component
public class SubscribeListener implements ApplicationListener<SessionSubscribeEvent> {
@Autowired
AbstractSubscribableChannel clientOutboundChannel;
@Override
public void onApplicationEvent(SessionSubscribeEvent event) {
Message<byte[]> message = event.getMessage();
StompHeaderAccessor stompHeaderAccessor = StompHeaderAccessor.wrap(message);
if (stompHeaderAccessor.getReceipt() != null) {
StompHeaderAccessor receipt = StompHeaderAccessor.create(StompCommand.RECEIPT);
receipt.setReceiptId(stompHeaderAccessor.getReceipt());
receipt.setSessionId(stompHeaderAccessor.getSessionId());
clientOutboundChannel.send(MessageBuilder.createMessage(new byte[0], receipt.getMessageHeaders()));
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With