Spring WebSocket MessageMapping not working
Asked Answered
C

1

9

I need to write chat. But I have a problem, I can't trigger MessageMapping controller. App contains of 2 parts, front-end which runs on 4200 port and back-end which is running on 8080 port. I have set all the configuration, but @MessageMapping controller is not getting triggered. And back-end console says nothing. But the browser console says, that message is sent. Can you help me please with solution. Here are hava configs.

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

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

Here is the controller

@Controller
public class MessageController {


@MessageMapping("/chat")
@SendTo("/topic/message")
public void sendMessage(Message message) {
    System.out.println("test");
}

And the front end part which is written on angular :

(function (angular, SockJS, Stomp, _, undefined) {
"use strict";

angular.module("app").
factory("chatService", chatService);

chatService.$inject = ["$q", "$timeout"];

function chatService ($q, $timeout) {

    var service = {}, listener = $q.defer(), socket = {
        client: null,
        stomp: null
    }, messageIds = [];

    service.RECONNECT_TIMEOUT = 30000;
    service.SOCKET_URL = "http://localhost:8080/chat";
    service.CHAT_TOPIC = "http://localhost:8080/topic/message";
    service.CHAT_BROKER = "http://localhost:8080/app/chat";

    service.receive = function() {
        return listener.promise;
    };

    service.send = function(message) {
        var id = Math.floor(Math.random() * 1000000);
        socket.stomp.send(service.CHAT_BROKER, {
            priority: 9
        }, JSON.stringify({
            message: message,
            id: id
        }));
        messageIds.push(id);
    };

    var reconnect = function() {
        $timeout(function() {
            initialize();
        }, this.RECONNECT_TIMEOUT);
    };

    var getMessage = function(data) {
        var message = JSON.parse(data), out = {};
        out.message = message.message;
        out.time = new Date(message.time);
        if (_.contains(messageIds, message.id)) {
            out.self = true;
            messageIds = _.remove(messageIds, message.id);
        }
        return out;
    };

    var startListener = function() {
        socket.stomp.subscribe(service.CHAT_TOPIC, function(data) {
            listener.notify(getMessage(data.body));
        });
    };

    var initialize = function() {
        socket.client = new SockJS(service.SOCKET_URL);
        socket.stomp = Stomp.over(socket.client);
        socket.stomp.connect({}, startListener);
        socket.stomp.onclose = reconnect;
    };

    initialize();
    return service;
}

 })(angular, SockJS, Stomp, _);

The browser console sais this :

Opening Web Socket...
    Web Socket Opened...

    >>> CONNECT
    accept-version:1.1,1.0
    heart-beat:10000,10000


    version:1.1
    heart-beat:0,0
    user-name:[email protected]

    connected to server undefined
    vendor-cd0f7ffee3.js:113305
    >>> SUBSCRIBE
    id:sub-0
    destination:http://localhost:8080/topic/message

    >>> SEND
    priority:9
    destination:http://localhost:8080/app/chat
    content-length:30

    {"message":"test","id":490235}
Crash answered 3/9, 2017 at 12:20 Comment(0)
D
0

I had this same issue. Just incase it helps anyone in the future. I spent hours on this. Replacing this:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-websocket</artifactId>
    <version>5.3.8</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-messaging</artifactId>
    <version>5.3.8</version>
</dependency>

With:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>webjars-locator-core</artifactId>
</dependency>
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>sockjs-client</artifactId>
    <version>1.0.2</version>
</dependency>
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>stomp-websocket</artifactId>
    <version>2.3.3</version>
</dependency>

Did the trick for me.

Disappointment answered 28/6, 2021 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.