Search in sources :

Example 1 with SessionConnectedEvent

use of org.springframework.web.socket.messaging.SessionConnectedEvent 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 SessionConnectedEvent

use of org.springframework.web.socket.messaging.SessionConnectedEvent in project spring-integration by spring-projects.

the class StompIntegrationTests method sendMessageToController.

@Test
public void sendMessageToController() throws Exception {
    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECT);
    this.webSocketOutputChannel.send(MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build());
    Message<?> receive = this.webSocketEvents.receive(20000);
    assertNotNull(receive);
    Object event = receive.getPayload();
    assertThat(event, instanceOf(SessionConnectedEvent.class));
    Message<?> connectedMessage = ((SessionConnectedEvent) event).getMessage();
    headers = StompHeaderAccessor.wrap(connectedMessage);
    assertEquals(StompCommand.CONNECTED, headers.getCommand());
    headers = StompHeaderAccessor.create(StompCommand.SEND);
    headers.setSubscriptionId("sub1");
    headers.setDestination("/app/simple");
    Message<String> message = MessageBuilder.withPayload("foo").setHeaders(headers).build();
    this.webSocketOutputChannel.send(message);
    SimpleController controller = this.serverContext.getBean(SimpleController.class);
    assertTrue(controller.latch.await(20, TimeUnit.SECONDS));
    assertEquals(StompCommand.SEND.name(), controller.stompCommand);
}
Also used : SessionConnectedEvent(org.springframework.web.socket.messaging.SessionConnectedEvent) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) Test(org.junit.Test)

Aggregations

SessionConnectedEvent (org.springframework.web.socket.messaging.SessionConnectedEvent)2 Test (org.junit.Test)1 ReceiptEvent (org.springframework.integration.websocket.event.ReceiptEvent)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