使用Stomp将多条消息发送到同一主题

The*_*heo 5 spring annotations stomp websocket

所以我正试图从服务器向我的浏览器发送消息,并有两个方法应该被连线以将消息发送到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 + "!");
}
Run Code Online (Sandbox Code Playgroud)

第一个也映射为接收消息并发回一个消息.第一个功能工作,消息将其返回到浏览器并显示在网页上.然而,第二次不起作用.它永远不会在网页的控制台中显示收到的消息.我只能用一种方法发送到同一主题吗?我尝试更改主题并向我的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);
            });
        });
    }
Run Code Online (Sandbox Code Playgroud)

这是我的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();
    }

}
Run Code Online (Sandbox Code Playgroud)

Ram*_*res 5

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

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

STOMP服务器可以使用MESSAGE命令向所有订户广播消息.

知道服务器无法发送未经请求的消息非常重要.*

来自服务器的所有消息必须响应特定的客户端订阅,并且服务器消息的"subscription-id"头必须与客户端订阅的"id"头匹配.

所以我猜第一种方法是有效的,因为它是先前映射的响应.


好的,我用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 + "!"));      
  }   
}
...
Run Code Online (Sandbox Code Playgroud)