Websocket Stomp - 广播(主题,队列)

时间:2014-12-04 11:06:15

标签: spring websocket stomp

如何向所有subscribers(Topic)和指定的user(Channel)广播。

this.messagingTemplate.convertAndSend(destination, message);
this.messagingTemplate.convertAndSendToUser(userId, destination, message);

这是对的吗?

什么是WebSocketConnectHandlerDecoratorFactory课程?

public final class WebSocketConnectHandlerDecoratorFactory implements WebSocketHandlerDecoratorFactory {

    private static final Log logger = LogFactory.getLog(WebSocketConnectHandlerDecoratorFactory.class);

    private final ApplicationEventPublisher eventPublisher;

    /**
     * Creates a new instance
     *
     * @param eventPublisher the {@link ApplicationEventPublisher} to use. Cannot be null.
     */
    public WebSocketConnectHandlerDecoratorFactory(ApplicationEventPublisher eventPublisher) {
        Assert.notNull(eventPublisher, "eventPublisher cannot be null");
        this.eventPublisher = eventPublisher;
    }

    @Override
    public WebSocketHandler decorate(WebSocketHandler handler) {
        return new SessionWebSocketHandler(handler);
    }

    private final class SessionWebSocketHandler extends WebSocketHandlerDecorator {

        public SessionWebSocketHandler(WebSocketHandler delegate) {
            super(delegate);
        }

        @Override
        public void afterConnectionEstablished(WebSocketSession wsSession)
                throws Exception {
            super.afterConnectionEstablished(wsSession);

            publishEvent(new SessionConnectEvent(this,wsSession));
        }

        private void publishEvent(ApplicationEvent event) {
            try {
                eventPublisher.publishEvent(event);
            }
            catch (Throwable ex) {
                logger.error("Error publishing " + event + ".", ex);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

  1. 正确。

  2. 查看其JavaDocs

    /**
     * Ensures that a {@link SessionConnectEvent} is published in
     * {@link WebSocketHandler#afterConnectionEstablished(WebSocketSession)}. This
     * is necessary so that the {@link WebSocketSession} can be mapped to the
     * corresponding Spring {@link Session} to terminate any
     * {@link WebSocketSession} associated with a Spring {@link Session} that was
     * destroyed.
     *
     * @author Rob Winch
     * @since 1.0
     *
     * @see WebSocketRegistryListener
     */