Search in sources :

Example 1 with ReceiptEvent

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);
        }
    }
}
Also used : ReceiptEvent(org.springframework.integration.websocket.event.ReceiptEvent) SimpMessageType(org.springframework.messaging.simp.SimpMessageType) Message(org.springframework.messaging.Message) WebSocketMessage(org.springframework.web.socket.WebSocketMessage) SessionConnectedEvent(org.springframework.web.socket.messaging.SessionConnectedEvent) StompCommand(org.springframework.messaging.simp.stomp.StompCommand) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) WebSocketSession(org.springframework.web.socket.WebSocketSession)

Example 2 with ReceiptEvent

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());
}
Also used : ReceiptEvent(org.springframework.integration.websocket.event.ReceiptEvent) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) Test(org.junit.Test)

Aggregations

ReceiptEvent (org.springframework.integration.websocket.event.ReceiptEvent)2 Test (org.junit.Test)1 Message (org.springframework.messaging.Message)1 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)1 SimpMessageType (org.springframework.messaging.simp.SimpMessageType)1 StompCommand (org.springframework.messaging.simp.stomp.StompCommand)1 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)1 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)1 WebSocketSession (org.springframework.web.socket.WebSocketSession)1 SessionConnectedEvent (org.springframework.web.socket.messaging.SessionConnectedEvent)1