0

I have the following code...

@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>

The Client side code seems to connect fine but I don't see the console message meaning to me "/stomp/app/hello" is the wrong path. What should the proper path be?

I also tried /app/stomp/hello no dice...

Update

I can remove the @RequestMapping("/stomp/**") and remove the stomp related stuff and it works fine for my simple test, however, I need it to work for a more complex application that will not allow this.

Jackie
  • 21,969
  • 32
  • 147
  • 289

1 Answers1

1

@RequestMapping and @MessageMapping annotations can be used in similar ways, but are totally different. @MessageMapping can also be used at the type level (see reference documentation), so you could annotate your controller with @MessageMapping("/stomp/**").

Nothing prevents you from annotating a Controller with both @MessageMapping and @RequestMapping - similar programming model, different purposes.

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176