use of org.springframework.integration.websocket.event.ReceiptEvent in project spring-integration by spring-projects.
the class WebSocketInboundChannelAdapter method handleMessageAndSend.
@SuppressWarnings("unchecked")
private void handleMessageAndSend(Message<?> message) throws Exception {
SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.wrap(message);
StompCommand stompCommand = (StompCommand) headerAccessor.getHeader("stompCommand");
SimpMessageType messageType = headerAccessor.getMessageType();
if ((messageType == null || SimpMessageType.MESSAGE.equals(messageType) || (SimpMessageType.CONNECT.equals(messageType) && !this.useBroker) || StompCommand.CONNECTED.equals(stompCommand) || StompCommand.RECEIPT.equals(stompCommand)) && !checkDestinationPrefix(headerAccessor.getDestination())) {
if (SimpMessageType.CONNECT.equals(messageType)) {
String sessionId = headerAccessor.getSessionId();
SimpMessageHeaderAccessor connectAck = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK);
connectAck.setSessionId(sessionId);
connectAck.setHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER, message);
Message<byte[]> ackMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, connectAck.getMessageHeaders());
WebSocketSession session = this.webSocketContainer.getSession(sessionId);
this.subProtocolHandlerRegistry.findProtocolHandler(session).handleMessageToClient(session, ackMessage);
} else if (StompCommand.CONNECTED.equals(stompCommand)) {
this.eventPublisher.publishEvent(new SessionConnectedEvent(this, (Message<byte[]>) message));
} else if (StompCommand.RECEIPT.equals(stompCommand)) {
this.eventPublisher.publishEvent(new ReceiptEvent(this, (Message<byte[]>) message));
} else {
headerAccessor.removeHeader(SimpMessageHeaderAccessor.NATIVE_HEADERS);
Object payload = this.messageConverter.fromMessage(message, this.payloadType.get());
sendMessage(getMessageBuilderFactory().withPayload(payload).copyHeaders(headerAccessor.toMap()).build());
}
} else {
if (this.useBroker) {
this.brokerHandler.handleMessage(message);
} else if (logger.isDebugEnabled()) {
logger.debug("Messages with non 'SimpMessageType.MESSAGE' type are ignored for sending to the " + "'outputChannel'. They have to be emitted as 'ApplicationEvent's " + "from the 'SubProtocolHandler'. Or using 'AbstractBrokerMessageHandler'(useBroker = true) " + "from server side. Received message: " + message);
}
}
}
use of org.springframework.integration.websocket.event.ReceiptEvent in project spring-integration by spring-projects.
the class StompIntegrationTests method sendMessageToControllerAndReceiveReplyViaTopic.
@Test
public void sendMessageToControllerAndReceiveReplyViaTopic() throws Exception {
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
headers.setSubscriptionId("subs1");
headers.setDestination("/topic/increment");
headers.setReceipt("myReceipt");
Message<byte[]> message = MessageBuilder.withPayload(ByteBuffer.allocate(0).array()).setHeaders(headers).build();
this.webSocketOutputChannel.send(message);
Message<?> receive = this.webSocketEvents.receive(20000);
assertNotNull(receive);
Object event = receive.getPayload();
assertThat(event, instanceOf(ReceiptEvent.class));
Message<?> receiptMessage = ((ReceiptEvent) event).getMessage();
headers = StompHeaderAccessor.wrap(receiptMessage);
assertEquals(StompCommand.RECEIPT, headers.getCommand());
assertEquals("myReceipt", headers.getReceiptId());
waitForSubscribe("/topic/increment");
headers = StompHeaderAccessor.create(StompCommand.SEND);
headers.setSubscriptionId("subs1");
headers.setDestination("/app/increment");
Message<Integer> message2 = MessageBuilder.withPayload(5).setHeaders(headers).build();
this.webSocketOutputChannel.send(message2);
receive = webSocketInputChannel.receive(20000);
assertNotNull(receive);
assertEquals("6", receive.getPayload());
}
Aggregations