Search in sources :

Example 11 with StompCommand

use of org.springframework.messaging.simp.stomp.StompCommand in project JavaForFun by gumartinm.

the class SessionConnectedListener method onApplicationEvent.

@Override
public void onApplicationEvent(SessionConnectedEvent event) {
    LOGGER.info("SessionConnectedEvent timestamp: " + event.getTimestamp());
    LOGGER.info("SessionConnectedEvent user: " + event.getUser());
    LOGGER.info("SessionConnectedEvent: " + event.toString());
    StompHeaderAccessor accessor = StompHeaderAccessor.wrap(event.getMessage());
    StompCommand command = accessor.getCommand();
    LOGGER.info("SessionConnectedEvent, StompCommand: " + command);
    LOGGER.info("SessionConnectedEvent, login: " + accessor.getLogin());
    long[] heartBeats = accessor.getHeartbeat();
    for (long heartBeat : heartBeats) {
        LOGGER.info("SessionConnectedEvent, heartBeat: " + heartBeat);
    }
    LOGGER.info("SessionConnectedEvent, destination: " + accessor.getDestination());
    LOGGER.info("SessionConnectedEvent, host: " + accessor.getHost());
    LOGGER.info("SessionConnectedEvent, message: " + accessor.getMessage());
    LOGGER.info("SessionConnectedEvent, sessionId: " + accessor.getSessionId());
    LOGGER.info("SessionConnectedEvent, subscriptionId: " + accessor.getSubscriptionId());
    byte[] payload = (byte[]) event.getMessage().getPayload();
    String stringPayload = new String(payload);
    LOGGER.info("SessionConnectedEvent, payload: " + stringPayload);
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) StompCommand(org.springframework.messaging.simp.stomp.StompCommand)

Example 12 with StompCommand

use of org.springframework.messaging.simp.stomp.StompCommand in project JavaForFun by gumartinm.

the class SessionDisconnectListener method onApplicationEvent.

@Override
public void onApplicationEvent(SessionDisconnectEvent event) {
    LOGGER.info("SessionDisconnectEvent timestamp: " + event.getTimestamp());
    LOGGER.info("SessionDisconnectEvent user: " + event.getUser());
    LOGGER.info("SessionDisconnectEvent sessionId: " + event.getSessionId());
    LOGGER.info("SessionDisconnectEvent close status: " + event.getCloseStatus());
    LOGGER.info("SessionDisconnectEvent: " + event.toString());
    StompHeaderAccessor accessor = StompHeaderAccessor.wrap(event.getMessage());
    StompCommand command = accessor.getCommand();
    LOGGER.info("SessionDisconnectEvent, StompCommand: " + command);
    LOGGER.info("SessionDisconnectEvent, login: " + accessor.getLogin());
    long[] heartBeats = accessor.getHeartbeat();
    for (long heartBeat : heartBeats) {
        LOGGER.info("SessionDisconnectEvent, heartBeat: " + heartBeat);
    }
    LOGGER.info("SessionDisconnectEvent, destination: " + accessor.getDestination());
    LOGGER.info("SessionDisconnectEvent, host: " + accessor.getHost());
    LOGGER.info("SessionDisconnectEvent, message: " + accessor.getMessage());
    LOGGER.info("SessionDisconnectEvent, sessionId: " + accessor.getSessionId());
    LOGGER.info("SessionDisconnectEvent, subscriptionId: " + accessor.getSubscriptionId());
    byte[] payload = (byte[]) event.getMessage().getPayload();
    String stringPayload = new String(payload);
    LOGGER.info("SessionDisconnectEvent, payload: " + stringPayload);
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) StompCommand(org.springframework.messaging.simp.stomp.StompCommand)

Example 13 with StompCommand

use of org.springframework.messaging.simp.stomp.StompCommand in project JavaForFun by gumartinm.

the class SessionSubscribeListener method onApplicationEvent.

@Override
public void onApplicationEvent(SessionSubscribeEvent event) {
    LOGGER.info("SessionSubscribeEvent timestamp: " + event.getTimestamp());
    LOGGER.info("SessionSubscribeEvent user: " + event.getUser());
    LOGGER.info("SessionSubscribeEvent: " + event.toString());
    StompHeaderAccessor accessor = StompHeaderAccessor.wrap(event.getMessage());
    StompCommand command = accessor.getCommand();
    LOGGER.info("SessionSubscribeEvent, StompCommand: " + command);
    LOGGER.info("SessionSubscribeEvent, login: " + accessor.getLogin());
    long[] heartBeats = accessor.getHeartbeat();
    for (long heartBeat : heartBeats) {
        LOGGER.info("CustomChannelInterceptor preSend, heartBeat: " + heartBeat);
    }
    LOGGER.info("SessionSubscribeEvent, destination: " + accessor.getDestination());
    LOGGER.info("SessionSubscribeEvent, host: " + accessor.getHost());
    LOGGER.info("SessionSubscribeEvent, message: " + accessor.getMessage());
    LOGGER.info("SessionSubscribeEvent, sessionId: " + accessor.getSessionId());
    LOGGER.info("SessionSubscribeEvent, subscriptionId: " + accessor.getSubscriptionId());
    byte[] payload = (byte[]) event.getMessage().getPayload();
    String stringPayload = new String(payload);
    LOGGER.info("SessionSubscribeEvent, payload: " + stringPayload);
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) StompCommand(org.springframework.messaging.simp.stomp.StompCommand)

Example 14 with StompCommand

use of org.springframework.messaging.simp.stomp.StompCommand in project spring-framework by spring-projects.

the class StompSubProtocolHandler method handleMessageToClient.

/**
 * Handle STOMP messages going back out to WebSocket clients.
 */
@Override
@SuppressWarnings("unchecked")
public void handleMessageToClient(WebSocketSession session, Message<?> message) {
    if (!(message.getPayload() instanceof byte[])) {
        if (logger.isErrorEnabled()) {
            logger.error("Expected byte[] payload. Ignoring " + message + ".");
        }
        return;
    }
    StompHeaderAccessor accessor = getStompHeaderAccessor(message);
    StompCommand command = accessor.getCommand();
    if (StompCommand.MESSAGE.equals(command)) {
        if (accessor.getSubscriptionId() == null && logger.isWarnEnabled()) {
            logger.warn("No STOMP \"subscription\" header in " + message);
        }
        String origDestination = accessor.getFirstNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION);
        if (origDestination != null) {
            accessor = toMutableAccessor(accessor, message);
            accessor.removeNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION);
            accessor.setDestination(origDestination);
        }
    } else if (StompCommand.CONNECTED.equals(command)) {
        this.stats.incrementConnectedCount();
        accessor = afterStompSessionConnected(message, accessor, session);
        if (this.eventPublisher != null) {
            try {
                SimpAttributes simpAttributes = new SimpAttributes(session.getId(), session.getAttributes());
                SimpAttributesContextHolder.setAttributes(simpAttributes);
                Principal user = getUser(session);
                publishEvent(this.eventPublisher, new SessionConnectedEvent(this, (Message<byte[]>) message, user));
            } finally {
                SimpAttributesContextHolder.resetAttributes();
            }
        }
    }
    byte[] payload = (byte[]) message.getPayload();
    if (StompCommand.ERROR.equals(command) && getErrorHandler() != null) {
        Message<byte[]> errorMessage = getErrorHandler().handleErrorMessageToClient((Message<byte[]>) message);
        if (errorMessage != null) {
            accessor = MessageHeaderAccessor.getAccessor(errorMessage, StompHeaderAccessor.class);
            Assert.state(accessor != null, "No StompHeaderAccessor");
            payload = errorMessage.getPayload();
        }
    }
    Runnable task = OrderedMessageChannelDecorator.getNextMessageTask(message);
    if (task != null) {
        Assert.isInstanceOf(ConcurrentWebSocketSessionDecorator.class, session);
        ((ConcurrentWebSocketSessionDecorator) session).setMessageCallback(m -> task.run());
    }
    sendToClient(session, accessor, payload);
}
Also used : SimpAttributes(org.springframework.messaging.simp.SimpAttributes) ConcurrentWebSocketSessionDecorator(org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) StompCommand(org.springframework.messaging.simp.stomp.StompCommand) Principal(java.security.Principal)

Aggregations

StompCommand (org.springframework.messaging.simp.stomp.StompCommand)14 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)10 Type (java.lang.reflect.Type)2 Principal (java.security.Principal)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ServerPortInfoApplicationContextInitializer (org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer)2 Message (org.springframework.messaging.Message)2 SimpleMessageConverter (org.springframework.messaging.converter.SimpleMessageConverter)2 StompFrameHandler (org.springframework.messaging.simp.stomp.StompFrameHandler)2 StompHeaders (org.springframework.messaging.simp.stomp.StompHeaders)2 StompSession (org.springframework.messaging.simp.stomp.StompSession)2 StompSessionHandler (org.springframework.messaging.simp.stomp.StompSessionHandler)2 StompSessionHandlerAdapter (org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter)2 BinaryMessage (org.springframework.web.socket.BinaryMessage)2 TextMessage (org.springframework.web.socket.TextMessage)2 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)2 WebSocketStompClient (org.springframework.web.socket.messaging.WebSocketStompClient)2 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1