0

我有以下代码...

@Controller
@RequestMapping("/stomp/**")
public class StompController {
    @MessageMapping("/hello")
    @SendTo("/topic/greet")
    public Greeting greet(HelloMessage message) throws Exception{
        System.out.println("Inside the method "+message.getName());
        Thread.sleep(3000);
        return new Greeting("Hello, "+message.getName()+"!");
    }
}
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/stomp/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

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

<script type="text/javascript">
    var stompClient = null;

    function setConnected(connected) {
        document.getElementById('connect').disabled = connected;
        document.getElementById('disconnect').disabled = !connected;
        document.getElementById('conversationDiv').style.visibility = connected ? 'visible' : 'hidden';
        document.getElementById('response').innerHTML = '';
    }

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

    function disconnect() {
        stompClient.disconnect();
        setConnected(false);
        console.log("Disconnected");
    }

    function sendName() {
        var name = document.getElementById('name').value;
        stompClient.send("/stomp/app/hello", {}, JSON.stringify({ 'name': name }));
    }

    function showGreeting(message) {
        var response = document.getElementById('response');
        var p = document.createElement('p');
        p.style.wordWrap = 'break-word';
        p.appendChild(document.createTextNode(message));
        response.appendChild(p);
    }
</script>

客户端代码似乎连接正常,但我没有看到控制台消息对我来说意味着“/stomp/app/hello”是错误的路径。正确的路径应该是什么?

我也试过 /app/stomp/hello no dice ...

更新

我可以删除@RequestMapping("/stomp/**")并删除与 stomp 相关的东西,它适用于我的简单测试,但是,我需要它适用于不允许这样做的更复杂的应用程序。

4

1 回答 1

1

@RequestMapping@MessageMapping注解可以以类似的方式使用,但完全不同。 @MessageMapping也可以在类型级别使用(请参阅参考文档),因此您可以使用@MessageMapping("/stomp/**").

没有什么可以阻止您使用两者来注释控制器 - 类似的@MessageMapping编程@RequestMapping模型,不同的目的。

于 2014-07-01T21:15:10.637 回答