【发布时间】: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