Spring WebSocket MessageMapping无效

时间:2017-09-03 12:20:15

标签: java websocket stomp java-websocket spring-messaging

我需要写聊天。但我有一个问题,我无法触发MessageMapping controller。应用程序包含2个部分,前端在 4200端口上运行,后端在 8080端口上运行。我已设置所有配置,但@MessageMapping controller未被触发。后端控制台什么也没说。但浏览器控制台说,该消息已发送。你能帮我解决一下吗?这是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();
}

这是控制器

@Controller
public class MessageController {


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

写在角上的前端部分:

(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, _);

浏览器控制台是这样的:

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:test@test.com

    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}

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。以防万一它可以帮助将来的任何人。我花了几个小时在这上面。替换这个:

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

与:

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

对我有用。