【问题标题】:Sending multiple messages to the same topic with Stomp使用 Stomp 向同一主题发送多条消息
【发布时间】:2014-08-29 18:46:36
【问题描述】:

所以我试图从服务器将消息发送回我的浏览器,并且有两种方法应该被连接以将消息发送到 STOMP 主题。

@MessageMapping("/hello")
@SendTo("/topic/greetings")
public static Greeting greeting(HelloMessage message) throws Exception {
    System.out.println("Sending message...");
    Application.startBody(message.getName());
    return new Greeting("Hello, " + message.getName() + "!");
}

@SendTo("/topic/greetings")
public static  Greeting security(String message) {
    System.out.println("entered the informer");
    return new Greeting("bye, " + message + "!");
}

第一个也映射为接收消息并发送回消息。第一个功能起作用,消息返回浏览器并显示在网页上。然而,第二次不起作用。它永远不会在网页的控制台中显示收到的消息。我只能用一种方法发送到同一个主题吗?我尝试更改主题并向我的 Stomp 客户端添加订阅,但这也不起作用。它与第二种方法是静态的有什么关系吗? (我需要从一个单独的类中调用它。)

这是我在 html 文件中的订阅:

    function connect() {
        var socket = new SockJS("http://localhost:8080/hello");
        stompClient = Stomp.over(socket);
        stompClient.connect({}, function(frame) {
            setConnected(true);
            console.log('Connected: ' + frame);
            stompClient.subscribe('/topic/greetings', function(greeting){
                showGreeting(JSON.parse(greeting.body).content);
            });
        });
    }

这是我的 WebSocketConfig.java:

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.setApplicationDestinationPrefixes("/app").enableSimpleBroker("/queue","/topic");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/hello").withSockJS();
    }

}

【问题讨论】:

    标签: spring websocket annotations stomp


    【解决方案1】:

    我有同样的问题并阅读了 Spring 的 WebShocket 支持,据说:

    http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html

    STOMP 服务器可以使用 MESSAGE 命令向所有订阅者广播消息。

    重要的是要知道服务器不能发送未经请求的消息。*

    来自服务器的所有消息都必须响应特定的客户端订阅,并且服务器消息的“subscription-id”标头必须与客户端订阅的“id”标头匹配。 p>

    所以我猜第一种方法可行,因为它是对先前映射的响应。


    好的,我用 SimpMessageSendingOperations 解决了这个问题

    @Controller
    public class PrincipalController {
    
    private static SimpMessageSendingOperations messagingTemplate;
    
    @Autowired
    public PrincipalController(SimpMessageSendingOperations messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }
    
    ...
    
      public static void security(String message){
          System.out.println("entered the informer");
          PrincipalController.messagingTemplate.convertAndSend("/topic/greetings", new     Greeting("bye, " +    message + "!"));      
      }   
    }
    ...
    

    【讨论】:

    • 太棒了!那么我如何从另一个班级发送消息呢?
    • 好吧,我将控制器保存在另一个类中以供以后使用。我还没有尝试过,但也许你可以将 Spring 的 @Component 注释和 @Autowire 控制器添加到你的类中。
    【解决方案2】:

    @Theo - 来自另一个班级

    只需自动绑定此服务

    @Service
    public class MessageService {
        @Autowired
        public SimpMessageSendingOperations messagingTemplate;
    
        public void sendMessage( String message ) {
    
            messagingTemplate.convertAndSend( "/topic/greetings", new Greeting( "bye, " + message   + "!" ) );
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-29
      • 2018-06-23
      • 2014-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多