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);
}
}
}
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);
}
Aggregations