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

问题描述:

如何向所有subscribers(Topic)和指定的user(Channel)广播。Websocket Stomp - 广播(主题,队列)

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. 正确的。

  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 
    */ 
    
+0

非常感谢!阿尔乔姆·比兰。 – Olivia 2014-12-09 07:08:47

+0

同样在这里@奥利维亚 – 2014-12-09 08:09:37