3
votes

I want to implement a websocket inside my projet to do something as notifications

this is my configuration web socket in my projet spring boot

 @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

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

and in my controller I have this method :

@MessageMapping("/hello")
    @SendTo("/topic/templatesWS")
    public void TemplatesWebSocket() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("calling juste");
    }

and in my client side in my page index.html I added this

<script src="//cdn.jsdelivr.net/sockjs/1/sockjs.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.js"></script>

and in my component angular2 I'm using this

declare let SockJS;
        declare let Stomp;


        var socket = new SockJS('http://localhost:8080/hello');
        socket.ono
        var stompClient = Stomp.over(socket);
            stompClient.connect({}, function(frame) {
            console.log('Connected: ' + frame);
            stompClient.subscribe('http://localhost:8080/template/topic/templatesWS', function(greeting) {
                console.log("from from", greeting);
            });
        }, function (err) {
            console.log('err', err);
        });

How can I call the method TemplatesWebSocket()

enter image description here

1
I think the subscribe() parameter should be simply '/topic/templatesWS' - Marcelus Trojahn
How send data to server Or get and object after an event ? - khalil _diouri
I resolve this error I have some configuration websocket security that prevent connection but the line code stompClient.subscribe not call the method from controller TemplatesWebSocket() how can I call it - khalil _diouri

1 Answers

1
votes

Check this example I just uploaded: https://github.com/mtrojahn/spring-boot-websocket

I think what you meant is send a command to the server and get a response, right? The example above covers both the response to a command sent by the client and also periodic messages sent to the clients.

You are probably looking for something like this:

stompClient.client.send("/app/somecommand", {}, "");

I hope it helps.