Spring 使用Stomp向同一主题发送多条消息

Spring 使用Stomp向同一主题发送多条消息,spring,websocket,annotations,stomp,Spring,Websocket,Annotations,Stomp,因此,我试图从服务器将消息发送回浏览器,并且有两种方法可以将消息发送到STOMP主题 @MessageMapping("/hello") @SendTo("/topic/greetings") public static Greeting greeting(HelloMessage message) throws Exception { System.out.println("Sending message..."); Application.startBody(message.

因此,我试图从服务器将消息发送回浏览器,并且有两种方法可以将消息发送到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的WebShocket支持时说:

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

必须知道服务器不能发送未经请求的消息。*

来自服务器的所有消息必须响应特定的客户端订阅,并且服务器消息的“订阅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 + "!"));      
  }   
}
...

@西奥-来自另一个班级

只需自动连线这项服务

@Service
public class MessageService {
    @Autowired
    public SimpMessageSendingOperations messagingTemplate;

    public void sendMessage( String message ) {

        messagingTemplate.convertAndSend( "/topic/greetings", new Greeting( "bye, " + message   + "!" ) );
    }
}

令人惊叹的!那么我如何从另一个类发送消息呢?我将控制器存储在另一个类中,以便以后使用。我还没有尝试过,但也许你可以将Spring的
@组件
注释和
@Autowire
控制器添加到你的类中。